我有一个 DBGrid,里面装满了访问数据库。我想要的只是像矩阵一样迭代该数据,例如单元格 00、01、02、10、11、12,
最佳答案
DBGrid 只是数据的可视化表示。 native 方法是迭代数据集和字段。
如果您的数据集确实支持 RecNo,您可以使用 RecNo 和字段索引访问这些值。(*)
确保不要访问高于 RecordCount - 1 的行和高于 FieldCount -1 的字段
var
i: Integer;
begin
Dataset.DisableControls;
try
Dataset.First;
While not Dataset.EOF do
begin
for i := 0 to Dataset.FieldCount - 1 do
begin
//Access your Field of the current row by depending of your intention by Value/String whatever you need
//DoSomeThingWith(Dataset.Fields[i].Value); // make sure to handle NULL values
DoSomeThingWith(Dataset.Fields[i].asString);
end;
Dataset.Next;
end;
finally
Dataset.EnableControls;
end;
end;
//(*)
Function GetFieldValue(Dataset:TDataset;Row:Integer;FieldIndex:Integer):Variant;
begin
Dataset.RecNo := Row;
Result := Dataset.Fields[FieldIndex].Value;
end;
关于delphi - 如何像delphi中的矩阵一样迭代DBGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376893/