Page 1 of 1

DataGrid Calling handler to modify dgData

Posted: Thu May 15, 2014 8:44 am
by Simon Knight
I have to admit that I don't understand or get on with datagrids, they just have not clicked (yet?). One of my recent ones went wrong and I would like to know how I should have coded it and as an aside where I can find a definitive list of all the messages that are sent to datagrid group.

What went wrong: I wanted a simple datagrid to store connection settings, the last column is named 'selected' and is either true or false. The datagrid has five rows and I want the setting of a row to true to set all the other rows to false i.e. only a single row may be set to true. To make things look better and easier to use I made the selected column a checkbox. My plan was to store a reference to the row that the user had set to true, then set all the values to false then set the row just being edited back to true. I placed the following in the column behavior for the column named 'selected':

Code: Select all

On mouseUp
   -- call routine on the card params name of dg, row data, theline number I think
   send "UpdateDG  the dgControl of the target, the dgData of the target, the dgline of me" to card "dbsettings" in 0
end mouseUp


I now recognise that I should have used the dgindex of me rather than dgline of me. Anyway the routine UpdateDG does the following:

Code: Select all

On UpdateDG pDgName,pDataA, pRowNo
   lock screen
  # set all the values of selected to false
   repeat for each key tkey in pDataA
      put false into pDataA[tkey]["selected"]
   end repeat
   
   # set the row of interest to true
   put true into pDataA[pRowNo]["selected"]
   answer pDgName
   set the dgData of  pDgName to pDataA
 
   unlock screen
end UpdateDG
Now this worked for a time. Then things became a little odd, this same 'design' was being used in three different stacks and after a while the datagrid on all of them stopped calling the mouseup in the column behavior. In fact the IDE inspector while showing that a column behavior was present (the button in the inspector was enabled the plus button disabled) refused to open it for editing. Deleting the button did not work and in the end I just deleted the datagrid and managed the values 'true' / 'false' by hand.

Looking at the Datagrid documents I suspect that I may be guilty of editing the dgdata before the datagrid is ready. So where should I put my call?

Once I have made this post I intend trying to put the call to UpdateDg inside the On SelectionChanged in the group script of the datagrid. But as I really have no idea what I should be doing and have already managed to create self destructing code I thought I would ask how the experts would do it.

Sorry for a long post and thanks for reading,
best wishes

Simon K

Re: DataGrid Calling handler to modify dgData

Posted: Thu May 15, 2014 5:03 pm
by Simon Knight
I have been experimenting and come up with a solution but would welcome comments. The column behavior has a mouseup handler which is called when the user changes the checkbox. The values of dgindex and the name of the datagrid are passed when a handler on the card UpdateDg is called. The time delay of 0.01 seconds is needed to ensure that the datagrid completes all its own housekeeping before UpdateDg makes changes. This reads the DgData of the datagrid and makes the required changes:

Column Behavior On MouseUp:

Code: Select all

on mouseUp pTheButton
if (pTheButton is 1) then
switch the short name of the target
case "Checkbox"
   SetDataOfIndex the dgIndex of me, the dgColumn of me, the hilited of button "Checkbox" of me
   RefreshIndex the dgIndex of me
            -- call handler on card
            put  the dgIndex of me into tIndex
            put  the dgData of me into tDataA
             put  the dgControl of the target into tDgName
            --UpdateDG tDgName, tDataA, tIndex
            send "UpdateDG" &&  tDgName && comma &&tIndex to card "dgTest" in 0.01 second
break
end switch
end if
end mouseUp
The handler UpdateDG on the card:

Code: Select all

On UpdateDG pDgName, pIndexNo
   -- called when the user makes a change to the datagrid
   --lock screen
   put the dgdata of pDGName into tDataA
  # set all the values of selected to false
   repeat for each key tkey in tDataA
      put false into tDataA[tkey]["Isselected"]
   end repeat
   
   # set the row of interest to true
   put true into tDataA[pIndexNo]["Isselected"]
   --answer pDgName
   set the dgData of  pDgName to tDataA
   --unlock screen
end UpdateDG
I did try passing dgData as a parameter to UpdateDg but the datagrid would stop displaying the contents correctly.

best wishes

Simon K