Delphi實現DBGrid全選和反選功能


    Delphi實現Dbgrid全選和反選、清除全選的功能,不管是在Delphi下,還是在WEB開發中,這種功能都是很實用的,是進行數據批量操作的基礎。本模塊就是實現了為Delphi的DBGrid數據列表增加全選內容、清除全選的功能,很實用了,代碼內容如下:

//全選
procedure TFrameCustSelector.ToolButton1Click(Sender: TObject);
var
  OldCurrent: TBookmark;
begin
  OldCurrent := DBGrid1.DataSource.DataSet.Bookmark;
  DBGrid1.DataSource.DataSet.DisableControls;
  DBGrid1.DataSource.DataSet.First ;
  while not DBGrid1.DataSource.DataSet.Eof do begin
    DBGrid1.SelectedRows.CurrentRowSelected := true;
    DBGrid1.DataSource.DataSet.Next;
  end;
  DBGrid1.DataSource.DataSet.GotoBookmark(OldCurrent);
  DBGrid1.DataSource.DataSet.EnableControls;
end;
//清除全選
procedure TFrameCustSelector.ToolButton2Click(Sender: TObject);
var
  OldCurrent: TBookmark;
begin
  OldCurrent := DBGrid1.DataSource.DataSet.Bookmark;
  DBGrid1.DataSource.DataSet.DisableControls;
  DBGrid1.DataSource.DataSet.First ;
  while not DBGrid1.DataSource.DataSet.Eof do begin
    DBGrid1.SelectedRows.CurrentRowSelected := False;
    DBGrid1.DataSource.DataSet.Next;
  end;
  DBGrid1.DataSource.DataSet.GotoBookmark(OldCurrent);
  DBGrid1.DataSource.DataSet.EnableControls;
end;
//反選
procedure TFrameCustSelector.ToolButton3Click(Sender: TObject);
var
OldCurrent: TBookmark;
begin
  OldCurrent := DBGrid1.DataSource.DataSet.Bookmark;
  DBGrid1.DataSource.DataSet.DisableControls;
  DBGrid1.DataSource.DataSet.First ;
  while not DBGrid1.DataSource.DataSet.Eof do begin
    DBGrid1.SelectedRows.CurrentRowSelected := not DBGrid1.SelectedRows.CurrentRowSelected;
    DBGrid1.DataSource.DataSet.Next;
  end;
  DBGrid1.DataSource.DataSet.GotoBookmark(OldCurrent);
  DBGrid1.DataSource.DataSet.EnableControls;
end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM