Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Get at data underneath TStringGrid selected row


This question is answered.


Permlink Replies: 7 - Last Post: Mar 13, 2016 1:28 PM Last Post By: Linden ROTH
Bob Johnson

Posts: 28
Registered: 9/11/15
Get at data underneath TStringGrid selected row  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2016 4:49 PM
I'm trying to understand how to get the data from the selected TStringGrid row. For example I would like to click on a Row and have the data from that row appear in Labels, etc below the TStringGrid. So take the data from each cell in the TStringGrid selected row and populate data items below.

Been struggling to figure this out. Would appreciate some help.

Thanks!
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: Get at data underneath TStringGrid selected row
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2016 6:09 PM   in response to: Bob Johnson in response to: Bob Johnson
Bob Johnson wrote:
I'm trying to understand how to get the data from the selected TStringGrid row. For example I would like to click on a Row and have the data from that row appear in Labels, etc below the TStringGrid. So take the data from each cell in the TStringGrid selected row and populate data items below.

Been struggling to figure this out. Would appreciate some help.

Thanks!

something like this maybe

procedure TForm12.StringGrid1SelectCell(Sender: TObject; const ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  label1.Text := StringGrid1.Cells[ 0, ARow ];
  label2.Text := StringGrid1.Cells[ 1, ARow ];
end;


--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
Bob Johnson

Posts: 28
Registered: 9/11/15
Re: Get at data underneath TStringGrid selected row  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2016 7:19 PM   in response to: Linden ROTH in response to: Linden ROTH
Linden ROTH wrote:
Bob Johnson wrote:
I'm trying to understand how to get the data from the selected TStringGrid row. For example I would like to click on a Row and have the data from that row appear in Labels, etc below the TStringGrid. So take the data from each cell in the TStringGrid selected row and populate data items below.

Been struggling to figure this out. Would appreciate some help.

Thanks!

something like this maybe

procedure TForm12.StringGrid1SelectCell(Sender: TObject; const ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  label1.Text := StringGrid1.Cells[ 0, ARow ];
  label2.Text := StringGrid1.Cells[ 1, ARow ];
end;


Wow! Yes that is exactly what I need. Thank you so much!
Bob Johnson

Posts: 28
Registered: 9/11/15
Re: Get at data underneath TStringGrid selected row  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 12, 2016 9:40 AM   in response to: Bob Johnson in response to: Bob Johnson
Bob Johnson wrote:
Linden ROTH wrote:
Bob Johnson wrote:
I'm trying to understand how to get the data from the selected TStringGrid row. For example I would like to click on a Row and have the data from that row appear in Labels, etc below the TStringGrid. So take the data from each cell in the TStringGrid selected row and populate data items below.

Been struggling to figure this out. Would appreciate some help.

Thanks!

something like this maybe

procedure TForm12.StringGrid1SelectCell(Sender: TObject; const ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  label1.Text := StringGrid1.Cells[ 0, ARow ];
  label2.Text := StringGrid1.Cells[ 1, ARow ];
end;


Wow! Yes that is exactly what I need. Thank you so much!

A follow-up question to this:

If the user clicks outside of the rows of data that are in the TStringGrid they get an error message: "Row index, 6, out of bounds" (in this case only 3 rows are in the grid). How can I eliminate this error message?
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: Get at data underneath TStringGrid selected row  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 12, 2016 10:28 AM   in response to: Bob Johnson in response to: Bob Johnson
Bob Johnson wrote:

A follow-up question to this:

If the user clicks outside of the rows of data that are in the TStringGrid they get an error message: "Row index, 6, out of bounds" (in this case only 3 rows are in the grid). How can I eliminate this error message?

Bug in stringgrid really - should not call SelectCell IMHO on invalid row or at least should just pass ARow as -1

SO test the value of ARow before using it

  if ( ARow < 0 ) or ( ARow >= StringGrid1.RowCount ) then
    begin
//      what ever
    end
  else
    begin
      label1.Text := StringGrid1.Cells[ 0, ARow ];
      label2.Text := StringGrid1.Cells[ 1, ARow ];
    end


--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Bob Johnson

Posts: 28
Registered: 9/11/15
Re: Get at data underneath TStringGrid selected row  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 12, 2016 12:06 PM   in response to: Linden ROTH in response to: Linden ROTH
Linden ROTH wrote:
Bob Johnson wrote:

A follow-up question to this:

If the user clicks outside of the rows of data that are in the TStringGrid they get an error message: "Row index, 6, out of bounds" (in this case only 3 rows are in the grid). How can I eliminate this error message?

Bug in stringgrid really - should not call SelectCell IMHO on invalid row or at least should just pass ARow as -1

SO test the value of ARow before using it

  if ( ARow < 0 ) or ( ARow >= StringGrid1.RowCount ) then
    begin
//      what ever
    end
  else
    begin
      label1.Text := StringGrid1.Cells[ 0, ARow ];
      label2.Text := StringGrid1.Cells[ 1, ARow ];
    end


Testing the value of ARow did the trick!

Obviously, I am pretty new to Delphi and my programming experience is fairly limited. I am learning a ton but some days it is a slog trying to figure stuff out. So I really do appreciate the help and support you have given me!

Thank you!
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Get at data underneath TStringGrid selected row [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 13, 2016 5:34 AM   in response to: Linden ROTH in response to: Linden ROTH
Am 12.03.2016 um 19:30 schrieb Linden ROTH:
Bob Johnson wrote:

A follow-up question to this:

If the user clicks outside of the rows of data that are in the TStringGrid they get an error message: "Row index, 6, out of bounds" (in this case only 3 rows are in the grid). How can I eliminate this error message?

Bug in stringgrid really - should not call SelectCell IMHO on invalid row or at least should just pass ARow as -1

While I agree it shouldn't do this I wonder whether somebody already has
reported that bug to quality.embarcadero.com? Otherwise it will continue
to live on unfixed ;-)

Greetings

Markus
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: Get at data underneath TStringGrid selected row [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 13, 2016 1:28 PM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:
Am 12.03.2016 um 19:30 schrieb Linden ROTH:
Bob Johnson wrote:

A follow-up question to this:

If the user clicks outside of the rows of data that are in the TStringGrid they get an error message: "Row index, 6, out of bounds" (in this case only 3 rows are in the grid). How can I eliminate this error message?

Bug in stringgrid really - should not call SelectCell IMHO on invalid row or at least should just pass ARow as -1

While I agree it shouldn't do this I wonder whether somebody already has
reported that bug to quality.embarcadero.com? Otherwise it will continue
to live on unfixed ;-)

Greetings

Markus

Markus

I did so much work with the stock stringgrid in XE2 -> XE4 and reported many issues - this would be the least of them - I consider the StringGrid Broken and not worth using ... I use TMS and DevExpress grids these days and avoid TStringGrid - I'd even use a column ownerdraw Tlistbox before TStringGrid

So feel free but I suspect that internally (Embarcadero) the StringGrid is a lost cause as well :)

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02