【发布时间】:2020-01-08 13:01:14
【问题描述】:
这是我的第一篇文章,我还在学习很多关于 Delphi 和通用编程的知识。所以请放心教。
我正在尝试使用 Access 中的列名填充 TStringList。然后在 TStringGrid 中显示它们。我目前收到“需要数组类型”错误。但我担心可能还有更多。
procedure TFormDB1DataMapping.FieldNamesToGrid();
var
myFieldnames: TStringList;
I: Integer;
begin
if not Form1.ConnIn1.Connected then begin
try
//Set Connection Parameters and connect
Form1.ConnIn1Parameters;
Form1.ConnIn1.Connected:=True
finally
end;
end;
myFieldnames := TStringList.Create;
Form1.ConnIn1.GetFieldNames('','',Form1.ComboBoxDB1TableName.Text,'',myFieldnames);
StringGridDB1.RowCount := StringGridDB1.RowCount + 1;
for I:= StringGridDB1.RowCount - 1 downto 1 do
StringGridDB1.Rows[I] := StringGridDB1.Rows[I - 1];
StringGridDB1.Cols[0][1] := myFieldnames.Text;
myFieldnames.Free;
End;
下面有答案的工作流程
procedure TFormDB1DataMapping.FieldNamesToGrid();
var
myFieldnames: TStringList;
I: Integer;
begin
if not Form1.ConnIn1.Connected then begin
try
//Set Connection Parameters and connect
Form1.ConnIn1Parameters;
Form1.ConnIn1.Connected:=True
finally
end;
end;
myFieldnames := TStringList.Create;
Form1.ConnIn1.GetFieldNames('','',Form1.ComboBoxDB1TableName.Text,'',myFieldnames);
StringGridDB1.RowCount := StringGridDB1.RowCount + 1;
StringGridDB1.RowCount := myFieldnames.Count + 1;
for I := 0 to myFieldnames.Count - 1 do
StringGridDB1.Cells[0, I + 1] := myFieldnames[I];
myFieldnames.Free;
End;
【问题讨论】:
-
TStringList 是一个包含字符串列表的对象。它不是字符串数组。我认为 GetFieldNames 需要一个字符串数组。
-
Dsm 感谢您的回答。获取字段名称来自 TFDConnection。错误发生在这里。 'StringGridDB1.Col[1,myFieldnames];//:= myFieldnames;'
-
我不确定您要达到的目标。列名应出现在 StringGrid 的什么位置?
-
你到底想做什么?你卡在哪里了?
-
你的代码在遇到异常时会泄露。你需要了解 try/finally。
标签: delphi