DataGrid problem

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

DataGrid problem

Post by CAsba » Fri Mar 22, 2024 11:36 am

Hi,
I'm troubleshooting a problem, updating the stock column in DG3, and to make it simple I've 'condensed' it into the following code, activated when the user selects a line (row).

Code: Select all

on selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes
   
   send "mouseup" to btn "extract" of cd "manage products"#gets data into fields
   send "mouseup" to btn "setdg"#shows columns appropriate to one of the fields
   
   put fld "stockused" of cd "manage products" into stockused
   answer fld "stockused"#works OK
   put the dgHilitedIndex of grp "datagrid 3" of cd "manage products" into tIndex
   put the dgDataOfIndex[tIndex] of grp "datagrid 3" of cd "manage products" into tArray   
   put 3 into tArray["stock"]
   put tArray["stock"] into fld "stockused"
   answer fld "stockused"#shows 3, all OK
   set the dgDataOfIndex[tIndex] of grp "datagrid 3" of cd "manage products" to tArray
   
end selectionChanged
I'm expecting the 'stock' column to be updated but it doesn't happen. Any ideas why ?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: DataGrid problem

Post by Klaus » Fri Mar 22, 2024 12:21 pm

Hi CAsba,

you could use "refreshindex", "refreshline" or "refreshlist" as the last line in your handler to force an update of the datagrid.
See the dictionary for the exact syntax of these commands.

Best

Klaus

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: DataGrid problem

Post by CAsba » Fri Mar 22, 2024 1:00 pm

Hi Klaus,
Many thanks. I think I wasted your time, though, as, experimenting further, I discovered that the two commands before the code were reponsible for the failure; by commenting them out, the code worked as expected.
Good to know you're still there !

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: DataGrid problem

Post by Klaus » Fri Mar 22, 2024 1:01 pm

CAsba wrote:
Fri Mar 22, 2024 1:00 pm
Good to know you're still there !
I'm ALWAYS here! :)

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: DataGrid problem

Post by stam » Fri Mar 22, 2024 1:08 pm

Klaus wrote:
Fri Mar 22, 2024 12:21 pm
you could use "refreshindex", "refreshline" or "refreshlist" as the last line in your handler to force an update of the datagrid.
See the dictionary for the exact syntax of these commands.
Worth trying what Klaus says, but this should work as is. The fact that it doesn't suggests there may or may not be other issues.
Setting the dgDataOfIndex should automatically refresh the datagrid - the dictionary states this in for example the setDataOfIndex entry:
Use dgDataOfIndex if you want to automatically refresh the data grid when you update the data.
What will say is that you should not need to use the dghilitedIndex - the selectionChanged already provides this as the pHilitedIndexes, so just use that;)
Also, if all your referenced controls are on the same card you're on, you don't need to add "of card" - keep code as simple as possible.

Stripping down your handler to its bare essentials, this should work:

Code: Select all

on selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes
    local tArray
    put the dgDataOfIndex[pHilitedIndexes] of grp "datagrid 3" into tArray   
    put 3 into tArray["stock"]
    set the dgDataOfIndex[pHilitedIndexes] of grp "datagrid 3" to tArray
 end selectionChanged

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: DataGrid problem

Post by CAsba » Fri Mar 22, 2024 1:29 pm

Hi Klaus,
I tried your suggestions without success, but (in case you're interested) I made the following workaround..

Code: Select all

on selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes
   if fld "indicator" is 0 then   
      send "mouseup" to btn "extract" of cd "manage products"#gets data into fields
      send "mouseup" to btn "setdg"#shows columns appropriate to one of the fields
      put 1 in fld "indicator"
      send "selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes" to grp "database 3"
      exit selectionchanged
      end if
      put fld "fcode" into tNewCost
      put the dgHilitedIndex of grp "datagrid 3" into tIndex
      put the dgDataOfIndex[tIndex] of grp "datagrid 3" into tArray
      put tNewCost into tArray["stock"]
      set the dgDataOfIndex[tIndex] of grp "datagrid 3" to tArray
     
      
      exit selectionchanged
  
      if fld "edit2" is 0 and fld "deleting" is 0 then
         
         send "mouseup" to btn "dgcode" of cd "manage products"
         
      end if
   end selectionChanged

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: DataGrid problem

Post by Klaus » Fri Mar 22, 2024 1:41 pm

Hint:

Code: Select all

exit <name of handler>

will really EXIT the handler at that point, so the following lines will never be executed!

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: DataGrid problem

Post by stam » Fri Mar 22, 2024 9:23 pm

CAsba wrote:
Fri Mar 22, 2024 1:29 pm

Code: Select all

on selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes
   if fld "indicator" is 0 then   
      send "mouseup" to btn "extract" of cd "manage products"#gets data into fields
      send "mouseup" to btn "setdg"#shows columns appropriate to one of the fields
      put 1 in fld "indicator"
      send "selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes" to grp "database 3"
      exit selectionchanged
      end if
      put fld "fcode" into tNewCost
      put the dgHilitedIndex of grp "datagrid 3" into tIndex
      put the dgDataOfIndex[tIndex] of grp "datagrid 3" into tArray
      put tNewCost into tArray["stock"]
      set the dgDataOfIndex[tIndex] of grp "datagrid 3" to tArray
     
      
      exit selectionchanged
  
      if fld "edit2" is 0 and fld "deleting" is 0 then
         
         send "mouseup" to btn "dgcode" of cd "manage products"
         
      end if
   end selectionChanged
As Klaus says the handler will stop executing and exit at line 7 if the first condition is true or at line 16 if not. "exit" should only be used if you want code execution to stop and putting code after this is nonsensical. Anything after this will not execute if you have reached the "exit handler" line.



My personal preferences in coding is to parcel out code into handlers in card or stack script so they can be referenced from anywhere, instead of sending mouseUps to buttons. I also prefer to use script local variables instead of fields for true/false flags (which I presume is what the 0 and 1 values refer to). This helps keep code clean, readable and easy to debug.

As already mentioned, there is no need to "put the dghilitedIndex of group DG3" - this is the pHilitedIndexes in the very first line.
Does the line: send "selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes" to grp "database 3" actually do anything?

Either it doesn't (because this is only triggered by user intervention, not by script)
Or it does something because you've put code in "database 3" selectionChanged handler that does something based on the index
But in itself, this will not hilite the same index and therefore any other code executed is probably better parcelled out into it's own handler that takes the index as a parameter.

Doing the above would make the code in the data grid's script easier to read. Your DG3 script could look exactly like this:

Code: Select all

local sIndicator, sEdit2, sDeleting
on selectionChanged pHilitedIndexes, pPreviouslyHilitedIndexes
    if not sIndicator then   
       extractAndSetDG
       put true into sIndicator
       dispatch "database3code" to group "database 3" with "pHilitedIndexes"
       exit selectionchanged // _ONLY_ if you want the code to stop here
    end if
    put the dgDataOfIndex[pHilitedIndexes] of grp "datagrid 3" into tArray
    put fld "fcode" into tArray["stock"]
    set the dgDataOfIndex[pHilitedIndexes] of grp "datagrid 3" to tArray
    if not sEdit2 and not sDeleting then dgCode
end selectionChanged
This, for me at least, is much easier to read and debug.

Instead of sending mouseUps to buttons, it refers to handlers in the card script:

Code: Select all

command extractAndSetDG
        // do stuff that is currently in btn "extract" and btn "setdg"
end extractAndSetDG

command dgCode
        // do stuff that is currently in button "dgCode"
end dgCode
and instead of sending a message that won't actually do much in the group "database 3":

Code: Select all

on database3code
        // do stuff that is currently in teh selectionChanged of group "database 3"
end database3code
But each to his own I suppose...

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”