Programmatically Selecting a Row or Cell in a DataGrid

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: 384
Joined: Fri Sep 30, 2022 12:11 pm

Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Wed Apr 19, 2023 3:27 pm

Hi,
In setting up an in-house manufactured product, my system automatically adds a new row to a data grid, which is a previously listed component of the manufactured product, re-listed, with a new code that combines the codes of both the manufactured product and the component product, to create a 'hybridcode'. This product, the hybridcode, has to be selected by the system to present a series of queries, the answers to which will be entered into the component products cells, relating to cut sizes and quantities, to arrive at a cost.
My problem is that I cannot programmitically select the row containing the hybridcode (which is referrable, in a field "hybridcode") to allow the user to seamlessly input the required information. This row will always be the last row (the last created) of the DG, which may offer an easy solution (which I haven't been able to arrive at). Any help would be most welcome and appreciated..

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by Klaus » Wed Apr 19, 2023 3:33 pm

Hi CAsba,

after you have added a new line, doe this:

Code: Select all

...
put the dgNumberOfLines of grp "your dg here..." into tLines
set the dgHilitedLines of grp "your dg here..." to tLines
...
:)

Best

Klaus

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by stam » Wed Apr 19, 2023 8:32 pm

Exactly what Klaus says - to select any line in a datagrid, use:

Code: Select all

set the dgHilitedLine of group "<data grid name>" to <integer>
just be aware than if you have code in the data grid's on selectionChanged pHilitedIndex, pPreviouslyHilitedIndex handler to do something when a line is highlighted, this will not fire when you programmatically change lines, so you need manage whatever action was in selectionChanged programmatically as well.

S.

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Fri Apr 21, 2023 4:25 pm

Hi Klaus and Stam,
Again, many thanks for your response.
Klaus, your solution worked perfectly, an easy method to hilite the last line.

Stam, I'm guessing the integer is the index - or line number - of the DG. I would have to know that first; is there a way to determine the line number of a line containing a particular code ?

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by Klaus » Fri Apr 21, 2023 5:11 pm

... is there a way to determine the line number of a line containing a particular code ?

Code: Select all

put lineoffset("your code here...",the dgtext of grp "your dg here") into tLineIamLookingFor

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by stam » Sat Apr 22, 2023 2:02 pm

CAsba wrote:
Fri Apr 21, 2023 4:25 pm
Hi Klaus and Stam,
Again, many thanks for your response.
Klaus, your solution worked perfectly, an easy method to hilite the last line.

Stam, I'm guessing the integer is the index - or line number - of the DG. I would have to know that first; is there a way to determine the line number of a line containing a particular code ?
Not sure I understand; but just in case:
You can get or set either the dgHilitedLine or the dgHilitedIndex.
The line from index and viceversa with the dgLineOfIndex and the dgIndexOfLine.

If you want to see which index is in which line in the IDE, you can use the property inspector panel switch to custom properties and select “dgCache” from the dropdown menu and you’ll get the array which populates the grid (with the index) and the order these indexes appear in (ie the index for each line).

Hopefully this helps with your question…

S.

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Sat Apr 22, 2023 2:44 pm

Mmm, what I was looking for was a way to programmitacally select a line, not knowing its index, but with reference only to a cell in a column named 'code'.

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by Klaus » Sat Apr 22, 2023 3:16 pm

See above...

To be a bit more precise:

Code: Select all

...
## put the known content of your column "code" into a variable e.g. tCode
## then:
put lineoffset(tCode,the dgtext of grp "your dg here") into tLineIamLookingFor
set the dghilitedlines of grp "your dg here..." to tLineIamLookingFor
...
I leave the neccessary error checking to you. :-D

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by stam » Sat Apr 22, 2023 5:43 pm

Or alternatively:
Put the dgData if the data grid into an array and use

Code: Select all

Filter elements of rArray where each [<“name of key”>] contains “<the text to search for>”
tArray will then contain only the the array elements that fulfill the search, including their numerical indices. So the keys of tArray will contain all the indices you’re searching for, 1 per line.

To get the line corresponding to each index, see dgLineOfIndex as I mentioned above.
Just an alternative approach…

HTH
S.

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Sun Apr 23, 2023 1:16 pm

Many thanks, and I now have line I wanted to select selected, which is great ! My next step is to edit the column 'Cost' by inserting the field fld "totalcost" into that cell, which I thought would be easy (mistakenly). I've tried many variations that I've looked up, and some I've tried to 'invent', but without success; so I have to shamelessly ask for help once more. In a nutshell, how do I programmatically edit the contents of the column 'cost', in the selected row by inserting the field "totalcost". Please..

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by Klaus » Sun Apr 23, 2023 2:35 pm

Et voila:

Code: Select all

...
put fld "totalcost" into tNewCost

## Will also work the same way with dgHilitedLines and dgDataOfLine
put the dgHilitedIndex of grp "your dg..." into tIndex

## Now get the data array of that index:
put the dgDataOfIndex[tIndex] into tArray

## Update column -> cost
put tNewCost into tArray["cost"]

## And write the data back into that index:
set the dgDataOfIndex[tIndex] to tArray
...
:)

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Sun Apr 23, 2023 4:32 pm

Hi Klaus,
Thanks
BUT
With this code

Code: Select all

 put fld "totalcost" into tNewCost
   
   ## Will also work the same way with dgHilitedLines and dgDataOfLine
   put the dgHilitedIndex of grp "datagrid 3" into tIndex
   
   ## Now get the data array of that index:
   put the dgDataOfIndex[tIndex] into tArray
   
   ## Update column -> cost
   put tNewCost into tArray["cost"]
   
   ## And write the data back into that index:
   set the dgDataOfIndex[tIndex] to tArray
I got ann error at

Code: Select all

 put the dgDataOfIndex[tIndex] into tArray
'Expression: bad factor, char 8'
Any ideas ?

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by Klaus » Sun Apr 23, 2023 5:04 pm

Oops, sorry, foregot to add "... of grp "datagrid 3"!
Made in a hurry... :oops:

This will work:

Code: Select all

...
   put fld "totalcost" into tNewCost
   
   ## Will also work the same way with dgHilitedLines and dgDataOfLine
   put the dgHilitedIndex of grp "datagrid 3" into tIndex
   
   ## Now get the data array of that index:
   put the dgDataOfIndex[tIndex] of grp "datagrid 3" into tArray
   
   ## Update column -> cost
   put tNewCost into tArray["cost"]
   
   ## And write the data back into that index:
   set the dgDataOfIndex[tIndex] of grp "datagrid 3" to tArray
...

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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Post by CAsba » Sun Apr 23, 2023 6:24 pm

Thank you so much, Klaus - you're a lifsaver !

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”