2004. május 7., péntek

How to select all rows in a TDBGrid programmatically


Problem/Question/Abstract:

Can someone tell me how I can select all rows in a DBGrid in code by clicking a button?

Answer:

Something like this should work (untested):

procedure SelectAllinGrid(grid: TDBGrid);
var
  saveBK: TBookmark;
  i: integer;
begin
  with grid.DataSource.Dataset do
  begin
    for i := 0 to SelectedList.Count - 1 do
      FreeBookmark(SelectedList.Items[i]);
    SelectedList.Clear;
    saveBK := GetBookmark; { Save current record position }
    DisableControls;
    try
      First;
      while (not Eof) do
      begin
        SelectedList.Add(GetBookmark);
        Next;
      end;
    finally
      GotoBookmark(saveBK); { Restore original record position}
      Freebookmark(saveBK);
      EnableControls;
    end;
  end
end;

Nincsenek megjegyzések:

Megjegyzés küldése