delphi - TStringList 到 TStringGrid
问题描述
这是我的第一篇文章,我还在学习很多关于 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;
解决方案
假设您希望第一列中的字段名称保留标题的顶行,您可以使用以下代码执行此操作:
StringGrid1.RowCount := myFieldnames.Count + 1;
for I := 0 to myFieldnames.Count - 1 do
StringGrid1.Cells[0, I + 1] := myFieldnames[I];
推荐阅读
- sql - SQL 服务器问题 - Create Function 必须是批处理中的唯一语句
- r - R stats::aggregate() 的“找不到对象”错误:两个相似的测试结果不同?
- git - 断言当前 HEAD 指向 master 的命令?
- git - 为什么我不能 git 捆绑所有父母?
- animation - 最简单的动画会导致高 CPU 使用率和非常高的能量影响 SpriteKit
- android - 如何在 BIOS 中为 x86 windows 启用 VT-x 以运行模拟器
- linux - 如何在 Linux 中的 C++ 中获取 Raspberry Pi 板上第二个 SD 卡插入/移除的事件
- css - SVG 相互覆盖 CSS 样式
- php - 在 get_instance 期间,Codeigniter 上的多个数据库在自定义库上未按预期工作
- swift - 将 Date 设置为 DatePicker 和侦听器以更改值和删除 AR 前缀