Mega Code Archive

 
Categories / Delphi / VCL
 

[] Dbgridde çoklu seçim

//TDBGrid and Multi-Selecting Records // property SelectedRows: TBookmarkList read FBookmarks; // TBookmarkList = class // public {* The Count property returns the number of currently selected items in the DBGrid *} // property Count: Integer read GetCount; {* The Clear method will free all the selected records within the DBGrid and set the Count property to 0 *} // procedure Clear; {* The Delete method will delete all the selected rows from the dataset *} // procedure Delete; {* The Find method determines whether a bookmark is in the selected list. *} // function Find(const Item: TBookmarkStr; // var Index: Integer): Boolean; {* The IndexOf method returns the index of the bookmark within the Items property. *} // function IndexOf(const Item: TBookmarkStr): Integer; {* The Refresh method returns a boolean value to notify whether any orphans were dropped (deleted) during the time the record has been selected in the grid. The refresh method can be used to update the selected list to minimize the possibility of accessing a deleted record. *} // function Refresh: Boolean; True = orphans found {* The CurrentRowSelected property returns a boolean value and determines whether the current row is selected or not. *} // property CurrentRowSelected: Boolean // read GetCurrentRowSelected // write SetCurrentRowSelected; {* The Items property is a TStringList of TBookmarkStr *} // property Items[Index: Integer]: TBookmarkStr // read GetItem; default; // end; unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, Db, DBTables, StdCtrls; type TForm1 = class(TForm) Table1: TTable; DataSource1: TDataSource; DBGrid1: TDBGrid; btnCount: TButton; btnSelected: TButton; btnClear: TButton; btnDelete: TButton; btnSelect: TButton; btnGetBookmark: TButton; btnFreeBookmark: TButton; btnFind: TButton; procedure btnCountClick(Sender: TObject); procedure btnSelectedClick(Sender: TObject); procedure btnClearClick(Sender: TObject); procedure btnDeleteClick(Sender: TObject); procedure btnSelectClick(Sender: TObject); procedure btnGetBookmarkClick(Sender: TObject); procedure btnFreeBookmarkClick(Sender: TObject); procedure btnFindClick(Sender: TObject); private Bookmark: TBookmark; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} //Example of the Count property procedure TForm1.btnCountClick(Sender: TObject); begin if DBgrid1.SelectedRows.Count > 0 then begin ShowMessage(IntToStr(DBgrid1.SelectedRows.Count)); end; end; //Example of the CurrentRowSelected property procedure TForm1.btnSelectedClick(Sender: TObject); begin if DBgrid1.SelectedRows.CurrentRowSelected then ShowMessage('Selected'); end; //Example of the Clear Method procedure TForm1.btnClearClick(Sender: TObject); begin DBGrid1.SelectedRows.Clear; end; //Example of the Delete Method procedure TForm1.btnDeleteClick(Sender: TObject); begin DBGrid1.SelectedRows.Delete; end; procedure TForm1.btnSelectClick(Sender: TObject); var x: Integer; TempBookmark: TBookMark; begin DBGrid1.Datasource.Dataset.DisableControls; with DBgrid1.SelectedRows do if Count > 0 then begin TempBookmark := DBGrid1.Datasource.Dataset.GetBookmark; for x := 0 to Count - 1 do begin if IndexOf(Items[x]) > -1 then begin DBGrid1.Datasource.Dataset.Bookmark := Items[x]; ShowMessage(DBGrid1.Datasource.Dataset.Fields[1].AsString); end; end; DBGrid1.Datasource.Dataset.GotoBookmark(TempBookmark); DBGrid1.Datasource.Dataset.FreeBookmark(TempBookmark); end; DBGrid1.Datasource.Dataset.EnableControls; end; //Sets a bookmark procedure TForm1.btnGetBookmarkClick(Sender: TObject); begin Bookmark := DBGrid1.Datasource.Dataset.GetBookmark; end; // Free the bookmark procedure TForm1.btnFreeBookmarkClick(Sender: TObject); begin if Assigned(Bookmark) then begin DBGrid1.Datasource.Dataset.FreeBookmark(Bookmark); Bookmark := nil; end; end; procedure TForm1.btnFindClick(Sender: TObject); var Index: Integer; begin if Assigned(Bookmark) then begin if DBGrid1.SelectedRows.Find(TBookMarkStr(Bookmark), Index) then ShowMessage('Index in SelectedRows: ' + IntToStr(Index)); end; end; end. { Unit1.DFM Source } object Form1: TForm1 Left = 206 Top = 107 Width = 616 Height = 459 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object DBGrid1: TDBGrid Left = 48 Top = 112 Width = 505 Height = 281 DataSource = DataSource1 Options = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect] TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] end object btnCount: TButton Left = 40 Top = 32 Width = 75 Height = 25 Caption = 'Count' TabOrder = 1 OnClick = btnCountClick end object btnSelected: TButton Left = 144 Top = 32 Width = 75 Height = 25 Caption = 'Selected' TabOrder = 2 OnClick = btnSelectedClick end object btnClear: TButton Left = 248 Top = 32 Width = 75 Height = 25 Caption = 'Clear' TabOrder = 3 OnClick = btnClearClick end object btnDelete: TButton Left = 352 Top = 32 Width = 75 Height = 25 Caption = 'Delete' TabOrder = 4 OnClick = btnDeleteClick end object btnSelect: TButton Left = 464 Top = 32 Width = 75 Height = 25 Caption = 'Select' TabOrder = 5 OnClick = btnSelectClick end object btnGetBookmark: TButton Left = 144 Top = 72 Width = 75 Height = 25 Caption = 'GetBookmark' TabOrder = 6 OnClick = btnGetBookmarkClick end object btnFreeBookmark: TButton Left = 248 Top = 72 Width = 81 Height = 25 Caption = 'FreeBookmark' TabOrder = 7 OnClick = btnFreeBookmarkClick end object btnFind: TButton Left = 344 Top = 72 Width = 75 Height = 25 Caption = 'Find' TabOrder = 8 OnClick = btnFindClick end object Table1: TTable Active = True DatabaseName = 'DBDEMOS' TableName = 'employee.db' Left = 384 Top = 360 end object DataSource1: TDataSource DataSet = Table1 Left = 440 Top = 376 end end