循环一个cxgrid上的记录,并更新一个字段/列 [英] Loop through records on a cxgrid and update a field/column
问题描述
我无法弄清楚这一点。
也许在描述我的问题时我没有足够的具体。
我有cxGrid我已经应用过滤器选择一些记录。
然后我需要做的是单击一个列标题,然后为这些记录设置一个名为fldselected的字段为True。
你更新的q要求是直截了当的,像Devex的东西一样,这是
全部在OLH只要你可以找到你的方式。
找到目前匹配过滤器的行的一种方法是使用
cxGrid1DBTableView1.DataController.FilteredRecordIndex []
属性。然后,您可以在数据集中找到该记录,以某种方式使用
cxGrid1DBTableView1.DataController.LocateByKey()来处理该记录。
更新:此答案的原始版本假定数据集有整数ID字段。
由于OP已经说他使用GUID,所以我已经相应更新了。
假设TClientDataSet CDS1有字段Guid:TGuidField,Name:TStringfield,大小32
和Selected:TBooleanField,并连接到
a cxDBTableView,启用了TcxGrid的过滤。
-
确保将cxGrid1DBTableView1.DataController.KeyFieldNames设置为Guid。
-
将正常的TDBGrid添加到表单中,并将其指向相同的数据源作为TcxGrid。点
是为了方便验证代码是否符合要求。 -
将下面的代码添加到单元中,点cxDBTableView1的OnColumnHeaderClick
处理程序cxGrid1DBTableView1ColumnHeaderClick和窗体的FormCreate上的OnCreate。
编译器&运行
代码:
程序TForm1.cxGrid1DBTableView1ColumnHeaderClick(发件人:TcxGridTableView ;
AColumn:TcxGridColumn);
begin
如果AColumn = cxGrid1DBTableView1Name然后
ProcessFilteredRecords;
结束
procedure TForm1.FormCreate(Sender:TObject);
var
AGuid:TGuid;
i:整数;
lResult:Longint;
begin
CDS1.IndexFieldNames:='名称';
CDS1.CreateDataSet;
for i:= 0 to 6 do begin
lResult:= SysUtils.CreateGUID(AGuid);
CDS1.Insert;
CDS1.FieldByName('Name')。AsString:= Chr(Ord('A')+ i);
CDS1.FieldByName('Guid')。AsString:= GuidToString(AGuid);
CDS1.FieldByName('Selected')。AsBoolean:= False;
CDS1.Post;
结束
CDS1.First;
结束
程序TForm1.ProcessFilteredRecords;
var
V:Variant;
i,
索引:整数;
BM:TBookMark;
begin
BM:= CDS1.GetBookMark;
CDS1.DisableControls;
尝试
为i:= 0到cxGrid1DBTableView1.DataController.FilteredRecordCount - 1 do begin
索引:= cxGrid1DBTableView1.DataController.FilteredRecordIndex [i];
//接下来,获取行的GUID值
V:= cxGrid1DBTableView1.DataController.Values [Index,0];
如果cxGrid1DBTableView1.DataController.LocateByKey(V)然后开始
CDS1.Edit;
CDS1.FieldByName('Selected')。AsBoolean:= True;
CDS1.Post;
结束
结束
finally
CDS1.EnableControls;
CDS1.GotoBookmark(BM);
CDS1.FreeBookmark(BM);
结束
结束
I have a cxGrid where I apply a filter to select certain records. When that is done I want to be able to update a field/column in the grid to mark each record that is to be used for the next operation. I haven't been able to figure this out
Maybe I haven't been specific enough when describing my problem. I have the cxGrid where I have applied a filter selecting some records. What I then need to do is to click a columnheader and then have a field called fldselected set to True for these records.
What your updated q is asking for is straightforward and as usual with Devex stuff, it's all in the OLH as long as you can find your way into it.
A way to find which rows currently match the filter is to use the
cxGrid1DBTableView1.DataController.FilteredRecordIndex[]
property. You can then find that record in the dataset to process it in some way using
cxGrid1DBTableView1.DataController.LocateByKey().
Update: The original version of this answer assumed that the dataset had an integer ID field. As the OP has said he uses GUIDs instead, I've upddated it accordingly.
Assuming the TClientDataSet CDS1 has fields Guid : TGuidField, Name : TStringfield, size 32 and Selected : TBooleanField and is connected to a cxDBTableView, with filtering enabled, of a TcxGrid.
Make sure the cxGrid1DBTableView1.DataController.KeyFieldNames is set to 'Guid'.
Add a regular TDBGrid to the form and point it at the same datasource as the TcxGrid. The point of this is to make it easy to verify that the code is working as required.
Add the code below to the unit, and point cxDBTableView1's OnColumnHeaderClick at the handler cxGrid1DBTableView1ColumnHeaderClick, and the form's OnCreate at the FormCreate.
Compiler & run
Code:
procedure TForm1.cxGrid1DBTableView1ColumnHeaderClick(Sender: TcxGridTableView;
AColumn: TcxGridColumn);
begin
if AColumn = cxGrid1DBTableView1Name then
ProcessFilteredRecords;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
AGuid : TGuid;
i : Integer;
lResult : Longint;
begin
CDS1.IndexFieldNames := 'Name';
CDS1.CreateDataSet;
for i:= 0 to 6 do begin
lResult := SysUtils.CreateGUID(AGuid);
CDS1.Insert;
CDS1.FieldByName('Name').AsString := Chr(Ord('A') + i);
CDS1.FieldByName('Guid').AsString := GuidToString(AGuid);
CDS1.FieldByName('Selected').AsBoolean := False;
CDS1.Post;
end;
CDS1.First;
end;
procedure TForm1.ProcessFilteredRecords;
var
V : Variant;
i,
Index: Integer;
BM : TBookMark;
begin
BM := CDS1.GetBookMark;
CDS1.DisableControls;
try
for i := 0 to cxGrid1DBTableView1.DataController.FilteredRecordCount - 1 do begin
Index := cxGrid1DBTableView1.DataController.FilteredRecordIndex[i];
// Next, get the GUID value of the row
V := cxGrid1DBTableView1.DataController.Values[Index, 0];
if cxGrid1DBTableView1.DataController.LocateByKey(V) then begin
CDS1.Edit;
CDS1.FieldByName('Selected').AsBoolean := True;
CDS1.Post;
end;
end;
finally
CDS1.EnableControls;
CDS1.GotoBookmark(BM);
CDS1.FreeBookmark(BM);
end;
end;
这篇关于循环一个cxgrid上的记录,并更新一个字段/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!