Multi-selection in DBGrid

Posted in Databases

Set dgMultiSelect option of DBGrid to true and put next code to the Button.OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var
  X: Word;
  TempBookmark: TBookMark;
begin
  with DBGrid1.DataSource.DataSet do
  begin
    DisableControls;
    with DBGrid1.SelectedRows do
      if Count<>0 then
      begin
        TempBookmark:=GetBookmark;
        for X:=0 to Count-1 do
        begin
          if IndexOf(Items[X])>-1 then
          begin
            Bookmark:=Items[X];
            ShowMessage(Fields[1].AsString);
          end;
        end;
      end;
    GotoBookmark(TempBookmark);
    FreeBookmark(TempBookmark);
    EnableControls;
  end;
end;