【问题标题】:Delphi - reading the records of a database into a string gridDelphi - 将数据库的记录读入字符串网格
【发布时间】:2021-01-21 14:11:55
【问题描述】:

那么,如何将数据库的记录(来自 TADOTable 组件)写入字符串网格? (记录的字段都是字符串)

我尝试了类似的方法但无济于事:

procedure TfrmPuntehou.WriteToList(tbl: TADOTable;grid:TStringGrid);
var
  iNewRowCount:integer;
  i,j,m: Integer;
  const
  separator = ',';
begin
  tempList:= TStringList.Create;
  try
    tbl.First;
    while not (tbl.Eof) do
    begin
      tempList.Add(tbl['Car Number']+separator+tbl['Racer Name']+separator+tbl['Licence']);
      tbl.Next;
    end;
        for j:= 1 to (tempList.Count - 1) do
      begin
      grid.Rows[j].Text := tempList.Strings[(J-1)] ;
      end;
  finally
   tempList.Free;
  end;

      //fill the row numbers
      for m := 1 to grid.rowcount do
      begin
      grid.Cells[0,m]:= IntToStr(m);
      end;
end;

我试图在启动时获得的输出示例:(行号列不是数据库的一部分)

提前感谢您的帮助!
亲切的问候
PrimeBeat

【问题讨论】:

  • 您不想使用TDBGrid的任何具体原因?
  • 您应该使用 TADOQuery 而不是 TADOTable。您根本不需要使用中间 TStringList。您可以直接写入网格单元格。使用两个嵌入式 for 循环:一个在记录上,一个在列上。您可以将 stringgrid ColCount 设置为预先设置的字段数,但应在循环中通过记录进度扩展 RowCount(以避免首先查询记录数)。
  • @TomBrunberg 因为我已经在网格中编写了很多其他函数,我只需要做这件事,然后我的程序就完成了。所以我不工作让我重新开始......
  • @fpiette 如何使用 for 循环等实现这样的 ADOQuery?我可以要求一个如何做到这一点的代码示例吗?
  • "使用 for 循环等?"简单 - 不要使用 for 循环从数据集(TAdoTable、TAdoQuery 等)中检索数据,因为您不应该假设您知道有多少数据集行(尤其是在多用户数据集中)。使用while 循环,就像您自己的代码一样...

标签: database delphi ado tstringgrid


【解决方案1】:

您的工作量太大了。您根本不需要单独的字符串列表,而且您的代码可以简单得多。

var
  i, Row: Integer;
begin
  // Populate header row
  Grid.Cells[0, 0] := 'Row';
  Row := 0;

  for i := 0 to Tbl.FieldCount - 1 do
    Grid.Cells[i + 1, Row] := Tbl.Fields[i].FieldName; // The +1 skips the Row column
  Inc(Row);
  // Populate cells
  Tbl.First;
  while not Tbl.Eof do
  begin
    for i := 0 to Tbl.FieldCount - 1 do
    begin
      Grid.Cells[i, Row] := IntToStr(i);                // Populate Row number
      Grid.Cells[i + 1, Row] := Tbl.Fields[i].AsString; // Fill rest of row with table data
    end;
    Inc(Row);
    Tbl.Next;
  end;
end;

【讨论】:

  • 您对字段的 for 循环不正确。填充行号不正确,for循环内递增行不正确。
【解决方案2】:

这是一个使用 TADOQuery 和 StringGrid 的示例:

procedure TForm1.Button1Click(Sender: TObject);
var
    I    : Integer;
    ARow : Integer;
begin
    ADOConnection1.Open('user', 'pass');
    ADOQuery1.SQL.Text := 'SELECT * FROM dbo.Person';
    ADOQuery1.Open;
    if ADOQuery1.Eof then begin
        ShowMessage('Data not found');
        Exit;
    end;
    SGrid.RowCount := 1;
    SGrid.ColCount := ADOQuery1.Fields.Count + 1;
    // Create titles of row 0
    for I := 0 to ADOQuery1.Fields.Count - 1 do
        SGrid.Cells[I + 1, 0] := ADOQuery1.Fields[I].DisplayName;
    // Populate the cells with data from result set
    ARow := 1;
    while not ADOQuery1.Eof do begin
        Inc(ARow);
        SGrid.RowCount := ARow + 1;
        SGrid.Cells[0, ARow] := ARow.ToString;
        for I := 0 to ADOQuery1.Fields.Count - 1 do
            SGrid.Cells[I + 1, ARow] := ADOQuery1.Fields[I].AsString;
        ADOQuery1.Next;
    end;
end;

【讨论】:

    【解决方案3】:

    感谢Ken White的回答,我设法解决了这个问题!

    procedure TfrmPuntehou.WriteToList(tbl: TADOTable;grid:TStringGrid);
    var
      Row: Integer;
    begin
    
      tbl.Active:=True;
    
      Row := 1;
    
      // Populate cells
      Tbl.First;
      while not Tbl.Eof do
      begin
        grid.Cells[0,Row]:= IntToStr(Row);
        grid.Cells[1,Row]:= tbl.fields[0].AsString;
        grid.Cells[2,Row]:= tbl.fields[1].AsString;
        grid.Cells[3,Row]:= tbl.fields[2].AsString;
        Inc(Row);
        IncreaseRowCount(grid);
        Tbl.Next;
      end;
      tbl.Active:=false;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多