Delphi Grid: How to get selectedrows count on the fly

Using SelectedRows.Count, I can get the number of selected grid lines. For example, if a user selects 3 lines, I can put a button on the form and click to show me the selected number. Good.

BUT, how can I update the number of rows when the user selects them ("on the fly"). I have tried many events Gridlike OnColEnteror OnMouseDown. It seems that they only update the counter when the user clicks outside the data columns, and not the first time they select a row.

Not seeing the events associated with the ROWS change in the component Grid, I tried many events in the basic data query, but they were also inconsistent or often required clicking in certain places. The best result I found (actual code) was after scrolling through the request:

procedure TDataHerd10.QuCowsAfterScroll(DataSet: TDataSet);
begin
  if MenuOpt = 'UpdtInd' then MainView.NumSelEdit.Text:=
    IntToStr(MainView.CowSelGrid.SelectedRows.Count);
end;

This event seems to be lagging behind one and adding another score to its original state when the user refuses multiselect to return to one line.

It seems that with the right event, should I be able to count the selected rows for the report to the user when they select / deselect the rows?

+4
source share
2 answers

: , , .

, , , . DBGrid, TEdit, dbgrid ( dbgrid) 3 TButtons, OnClick.

, , dbgrid dbgrid OnDrawColumnCell . ( 700 , ), - gui , . ( Set SetelectedCount setter).

type
  TForm1 = class(TForm)
    [...]
  private
    FSelectedCount: Integer;
    procedure SetSelectedCount(const Value: Integer);
  public
    procedure ShowSelectedCount;
    property SelectedCount : Integer read FSelectedCount write SetSelectedCount;
  end;
 [...]

procedure TForm1.btnClearSelectedClick(Sender: TObject);
begin
  DBGrid1.SelectedRows.Clear;
end;

procedure TForm1.btnGetSelectedClick(Sender: TObject);
begin
  ShowSelectedCount;
end;

procedure TForm1.btnSetSelectedClick(Sender: TObject);
begin
  DBGrid1.SelectedRows.CurrentRowSelected := True;
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
    DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  SelectedCount := DBGrid1.SelectedRows.Count;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ActiveControl := Edit1;  // so the grid does not have focus when the form is first shown
  SelectedCount := -1;
end;

procedure TForm1.SetSelectedCount(const Value: Integer);
begin
  if FSelectedCount <> Value then begin
    FSelectedCount := Value;
    ShowSelectedCount;
  end;
end;

procedure TForm1.ShowSelectedCount;
begin
  Caption := IntToStr(DBGrid1.SelectedRows.Count);
end;

DataSet.AfterScroll -gui-, . , DBGrid, , , , , (, ) .

,

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  Caption := IntToStr(DBGrid1.SelectedRows.Count);
end;

, - , Shift + Down .

,

procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift:
    TShiftState);
begin
  Caption := IntToStr(DBGrid1.SelectedRows.Count);
end;

() . , Key, .

, , AfterScroll, q, , ( , ) , +1.

+3

@Martyn...

( " " ), Grid1.KeyUp, , . Grid1.MouseUp. , , .

0

Source: https://habr.com/ru/post/1668069/


All Articles