1 (edited by lara.0080 2024-03-29 14:09:35)

Topic: Why am I getting the previous value

Dear all,

I have added a table grid to extract a value from a foreign key for comparison. I appended the column containing the foreign key. However, when I use the following code:

 times.Label20.Caption := times.TableGrid1.Cells[6, selectedRow];

I notice that the label displays the number of the previously selected row instead. Can anyone shed light on why this is happening? Additionally, is there an alternative method to retrieve the foreign key from the selected row without displaying it?

and also when I add this line to (on change) in the table grid  I get an undeclared identifier!"   Why does this happen ???

Thanks

Re: Why am I getting the previous value

Hi Lara.0080,
As always, attaching your project helps enormously in trying to debug any problems.
You have not specified what event you have attached your code to but I suspect that you are using 'OnMouseDown';  try attaching the code to 'OnMouseUp' instead.
Derek.

3 (edited by lara.0080 2024-03-29 15:45:37)

Re: Why am I getting the previous value

Thanks, I changed the order of the code, and it worked perfectly. However, why do I get an 'undeclared identifier' error when I use this code in the table grid's 'on change' event?"

 times.Label20.Caption := times.TableGrid1.Cells[6, selectedRow];

"And it mentions the selectedrow exactly!"

Re: Why am I getting the previous value

An 'onchange' event is automatically triggered when you open any form. 
But at that stage, no row has actually been selected (ie, the 'selectedrow' is still at -1) which is why you get the error.
You could amend your code to test for this:

procedure Form1_TableGrid1_OnChange (Sender: TObject);
begin
  if form1.tablegrid1.selectedrow > -1 then form1.label1.caption := form1.tablegrid1.cells[1,form1.tablegrid1.selectedrow];
end;

If you subsequently make a change to the underlying data, the even will be triggered and the label.caption should display correctly.
Derek.

Re: Why am I getting the previous value

Thanks i got it .