Page 1 of 1

Programmatically Selecting a Row or Cell in a DataGrid

Posted: Wed Apr 19, 2023 3:27 pm
by CAsba
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..

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Wed Apr 19, 2023 3:33 pm
by Klaus
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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Wed Apr 19, 2023 8:32 pm
by stam
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.

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Fri Apr 21, 2023 4:25 pm
by CAsba
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 ?

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Fri Apr 21, 2023 5:11 pm
by Klaus
... 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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sat Apr 22, 2023 2:02 pm
by stam
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.

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sat Apr 22, 2023 2:44 pm
by CAsba
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'.

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sat Apr 22, 2023 3:16 pm
by Klaus
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

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sat Apr 22, 2023 5:43 pm
by stam
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.

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sun Apr 23, 2023 1:16 pm
by CAsba
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..

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sun Apr 23, 2023 2:35 pm
by Klaus
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
...
:)

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sun Apr 23, 2023 4:32 pm
by CAsba
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 ?

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sun Apr 23, 2023 5:04 pm
by Klaus
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
...

Re: Programmatically Selecting a Row or Cell in a DataGrid

Posted: Sun Apr 23, 2023 6:24 pm
by CAsba
Thank you so much, Klaus - you're a lifsaver !