首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TStringList到TStringGrid

TStringList到TStringGrid
EN

Stack Overflow用户
提问于 2020-01-08 21:01:15
回答 1查看 762关注 0票数 0

这是我的第一篇文章,我还在学习很多关于Delphi和一般编程的知识。所以请不要客气地教我。

我正在尝试使用Access中的列名填充TStringList。然后在TStringGrid中显示它们。我目前收到一个“需要数组类型”的错误。但我担心可能会有更多。

代码语言:javascript
复制
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;

带有以下答案的工作程序

代码语言:javascript
复制
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;

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-08 22:15:17

假设您希望第一列中的字段名称保留顶部行作为标题,则可以使用以下代码实现此目的:

代码语言:javascript
复制
StringGrid1.RowCount := myFieldnames.Count + 1;
for I := 0 to myFieldnames.Count - 1 do
  StringGrid1.Cells[0, I + 1] := myFieldnames[I];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59646438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档