how to code the itemchanged event and datawindows
-
25-06-2021 - |
Question
I am using PowerBuilder classic 12.5 am having difficulties in inserting, editing, creating and printing reports.
i have a datawindow, dw_NewEmployee
with dataobject, d_newrecord
which is update-able.
- should i use the columns to insert records through the columns or i create single line texts on the window object
- is the
itemchanged
event used on columns and rows on dataobject or on single line texts... I am having trouble figuring out how to implement validation rules. please give me an example to validate employeeID_Number
Solution
I see that you seem confused about the datawindow usage.
Let's try to summarize:
- you create a new datawindow
d_newrecord
(say it is a grid) based on a sql select in your database, sayselect id_number, name from employee
. - in the detail zone of the datawindow (that will be repeated for each record at runtime but that is only once in design), you need to put one column object for each column (here you will have
id_number
andname
) these objects are both to display existing data and receive user input for editing data and inserting new records. - don't forget to set the Rows / Update properties if you need to make the dw updatable.
- in the header zone of the datawindow you can have a static text associated to each column that is just here to display the column name, it does not concern table data.
- in some window object, you place a datawindow control
dw_newemployee
where the content of the datawindow will be painted, you setd_newrecord
as its dataobject. - you need to set at some point the transaction object of the dw, for example in the
open()
event of the window:
dw_newemployee.SetTransObject(sqlca)
dw_newemployee.Retreive() //if you are using some retreival arguments, don't forget to include them here
When you want to insert new data in your table (for example with a window button "add"), in the clicked()
event of the button you call dw_newemployee.InsertRow(0)
to insert at the end.
The ItemChanged()
event will be triggered after one cell will be modified, you will be given the row, item (a dwobject) and new data. By choosing the returned value of the event, you can accept or reject the new data.
Here is an example for a field validation in itemchanged()
event:
long ll_return_code = 0
string ls_column
ls_column = lower(dwo.name)
choose case ls_column
case "id_number"
if long(data) = 42 THEN
messagebox("validation error", "You cannot use 42 for the ID")
ll_return_code = 1 //reject and stay in cell
end if
case "name"
if data = "foobar" then
messagebox("validation error", "Do not use dummy value...")
ll_return_code = 2 //reject but allow to go elsewhere
end if
end choose
return ll_return_code