dbgrid 选中行
场景:怎么 得到 DBGrid选中行的数据
如何 得到 DBGrid选中行的数据
请教各位下面两个问题
1.当DBGrid的opion属性中的 option的 dgrowselect 设置为false时 如何得到选中值
2.dgrowselect 设置为true是 又如何得到选中行的各个列的值呢?
上述两种情况在DBGrid允许选择多行时又该如何处理?
在下新手,请各位多多指教,不胜感激!!
------解决方案--------------------
一般是你鼠标点到哪一行,其DataSet的指针就指到了什么位置
你可以直接通过DataSet.Fields[i].Value来得到结果
如果是选择多行的话,可以使用循环来做,加个判断
DataSet.First;
while not DataSet.Eof do
begin
if DBGrid1.SelectedRows.CurrentRowSelected then
begin
for i := 0 to DbGrid1.Columns.Count - 1 do
begin
DBGrid1.Columns.Items[i].Field.Value //获得值
end;
end;
DataSet1.Next;
end;
------解决方案--------------------
hubdog的宝典里的
TDBGrid的多选特性
// property SelectedRows: TBookmarkList read FBookmarks;
// TBookmarkList = class
// public
{* The Clear method will free all the selected records
within the DBGrid *}
// 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 Count property returns the number of currently
selected items in the DBGrid *}
// property Count: Integer read GetCount;
{* 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;
如何 得到 DBGrid选中行的数据
请教各位下面两个问题
1.当DBGrid的opion属性中的 option的 dgrowselect 设置为false时 如何得到选中值
2.dgrowselect 设置为true是 又如何得到选中行的各个列的值呢?
上述两种情况在DBGrid允许选择多行时又该如何处理?
在下新手,请各位多多指教,不胜感激!!
------解决方案--------------------
一般是你鼠标点到哪一行,其DataSet的指针就指到了什么位置
你可以直接通过DataSet.Fields[i].Value来得到结果
如果是选择多行的话,可以使用循环来做,加个判断
DataSet.First;
while not DataSet.Eof do
begin
if DBGrid1.SelectedRows.CurrentRowSelected then
begin
for i := 0 to DbGrid1.Columns.Count - 1 do
begin
DBGrid1.Columns.Items[i].Field.Value //获得值
end;
end;
DataSet1.Next;
end;
------解决方案--------------------
hubdog的宝典里的
TDBGrid的多选特性
// property SelectedRows: TBookmarkList read FBookmarks;
// TBookmarkList = class
// public
{* The Clear method will free all the selected records
within the DBGrid *}
// 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 Count property returns the number of currently
selected items in the DBGrid *}
// property Count: Integer read GetCount;
{* 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;
- Delphi(Pascal) code
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables; type TForm1 = class(TForm) Table1: TTable; DBGrid1: TDBGrid; Count: TButton; Selected: TButton; Clear: TButton; Delete: TButton; Select: TButton; GetBookMark: TButton; Find: TButton; FreeBookmark: TButton; DataSource1: TDataSource; procedure CountClick(Sender: TObject); procedure SelectedClick(Sender: TObject); procedure ClearClick(Sender: TObject); procedure DeleteClick(Sender: TObject); procedure SelectClick(Sender: TObject); procedure GetBookMarkClick(Sender: TObject); procedure FindClick(Sender: TObject); procedure FreeBookmarkClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Bookmark1: TBookmark; z: Integer; implementation {$R *.DFM} //Example of the Count property procedure TForm1.CountClick(Sender: TObject); begin if DBgrid1.SelectedRows.Count > 0 then begin showmessage(inttostr(DBgrid1.SelectedRows.Count)); end; end; //Example of the CurrentRowSelected property procedure TForm1.SelectedClick(Sender: TObject); begin if DBgrid1.SelectedRows.CurrentRowSelected then showmessage('Selected'); end; //Example of the Clear Method procedure TForm1.ClearClick(Sender: TObject); begin dbgrid1.SelectedRows.Clear; end; //Example of the Delete Method procedure TForm1.DeleteClick(Sender: TObject); begin DBgrid1.SelectedRows.Delete; end; {* This example iterates through the selected rows of the grid and displays the second field of the dataset. The Method DisableControls is used so that the DBGrid will not update when the dataset is changed. The last position of the dataset is saved as a TBookmark. The IndexOf method is called to check whether or not the bookmark is still existent. The decision of using the IndexOf method rather than the Refresh method should be determined by the specific application. *} procedure TForm1.SelectClick(Sender: TObject); var x: word; 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; {* This example allows you to set a bookmark and and then search for the bookmarked record within selected a record(s) within the DBGrid. *} //Sets a bookmark procedure TForm1.GetBookMarkClick(Sender: TObject); begin Bookmark1:= DBGrid1.Datasource.Dataset.GetBookmark; end; //Frees the bookmark procedure TForm1.FreeBookmarkClick(Sender: TObject); begin if assigned(Bookmark1) then begin DBGrid1.Datasource.Dataset.FreeBookmark(Bookmark1); Bookmark1:= nil; end; end; //Uses the Find method to locate the position of the //bookmarked record within the selected list in the DBGrid procedure TForm1.FindClick(Sender: TObject); begin if assigned(Bookmark1) then begin if DBGrid1.SelectedRows.Find(TBookMarkStr(Bookmark1),z) then showmessage(inttostr(z)); end; end; end.
------解决方案--------------------
dgrowselect 设置为false时,不能选中行
得到 DBGrid选中行的数据 是否指当前选中格所在行的数据?
如是是,它与dgrowselect 设置为true是一样的.
当前选中行就是当前数据表记录的位置
你可直接用:
FieldByName('字段名').AS类型,来得到当前选中行各字段的数据
如果是多选,可用:
var
i:integer;
begin
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
你的dataset.GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
这里用:FieldByName('字段名').AS类型 得到各字段的值;
end;
end;
------解决方案--------------------
bookmark就是在数据集里做一个标记,方便定位
GotoBookmark当然就是直接定位到指定标记的那一条记录上去了
------解决方案--------------------
GotoBookmark将当前记录转到你选中的记录
pointer是类型转换
因为DBGrid1.SelectedRows.Items[i]返回的是TBookmarkStr(String)类型
而GotoBookmark要的 TBookmark(Pointer)类型,所以要转一下
你也可写成:
GotoBookmark(TBookmark(DBGrid1.SelectedRows.Items[i]));