Jump to content
ewong

TDBGrid's SelectedRows count

Recommended Posts

Hi,

 

I am using Delphi 10.3.  I have a DBGrid that's connected to a Datasource (which is connected to a dataset).

 

I have DBGrid's rowselect and multiselect options set to true.

 

I have the following in the datasource's onDataChange event:

 


procedure TForm1.DataSource2DataChange(Sender: TObject; Field: TField);
var
  cnt : integer;

begin
 label1.Caption := 'DataSource2DataChange';
 cnt := dbgrid1.SelectedRows.Count;
 label2.Caption := 'Count = ' + IntToStr(cnt);
 end;

 

So when I run the form,  and I select the an entry in the grid,  I see "Count = 0" in the label2 caption.

 

When I select a different row,  it finally changes to Count = 1.  However, if I ctrl-Click on a 2nd row, the 'Count = 1"

remains.  When I Ctrl-Click on a third row, the label changes to "Count = 2"  (but since I've selected three items, it should be "Count = 3").

 

Can someone point out what I'm missing?  Have I misunderstood the SelectedRows property?

 

Thanks

 

Ed

 

 

Share this post


Link to post
4 hours ago, ewong said:

Can someone point out what I'm missing?  Have I misunderstood the SelectedRows property?

You're trying to show the change on the dbgrid while using an event on the dataSource that's is triggered before the selection. You should display the selected rows from the events of the grid. In this case as the selection can be made by mouse or keyboard you could use the events OnKeyUp and OnMouseUp for the grid:

 

procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift:
    TShiftState);
begin
  UpdateSelectedCount;
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift:
    TShiftState; X, Y: Integer);
begin
  UpdateSelectedCount;
end;

procedure TForm1.UpdateSelectedCount;
begin
 label2.Caption := 'Count = ' + IntToStr(dbgrid1.SelectedRows.Count);
end;

 

Share this post


Link to post

Watch the grid's indicator column: The current (active) row is marked with a triangle, while selected rows are marked with a dot. If the current row is also a selected row, the indicator shows a dot and a arrow.

 

You need to make the current row a selected row:

if DBGrid1.SelectedRows.Count = 0 then
    DBGrid1.SelectedRows.CurrentRowSelected := True;

 

Share this post


Link to post
11 minutes ago, Lajos Juhász said:

You're trying to show the change on the dbgrid while using an event on the dataSource that's is triggered before the selection. You should display the selected rows from the events of the grid. In this case as the selection can be made by mouse or keyboard you could use the events OnKeyUp and OnMouseUp for the grid:

 

 

 

That explains what I'm seeing.  Thanks!  I got the OnDataChange idea from Stackoverflow.

 

Ed

 

Share this post


Link to post
8 minutes ago, Achim Kalwa said:

Watch the grid's indicator column: The current (active) row is marked with a triangle, while selected rows are marked with a dot. If the current row is also a selected row, the indicator shows a dot and a arrow.

 

You need to make the current row a selected row:


if DBGrid1.SelectedRows.Count = 0 then
    DBGrid1.SelectedRows.CurrentRowSelected := True;

 

 

I was wondering what the arrow and dot meant.   That clears up that question.

 

Thanks!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×