Loading data to StringGrid

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ads32w
Posts: 2
Joined: Wed 09 Mar 2011 21:24

Loading data to StringGrid

Post by ads32w » Wed 09 Mar 2011 21:31

I have on my form MSConnection1, MSQuery1, and DataSource1.

How to load data to StringGrid1?

JensFudge
Posts: 55
Joined: Mon 12 Jan 2009 08:37

Loading Data in a grid

Post by JensFudge » Thu 10 Mar 2011 07:33

Hi

There are different ways:

Use a DBgrid in stead of a stringgrid. A Dbgrid has a property Datasource, so you connect the dbgrid to the datasource, the datasource to the Dataset (TMSQuery or TMSTable), and the dataset to the connection. Upon setting everything to retreive data, the grid is automatically populated.
Be aware there are different dbaware grids apart from the one shipping with Delphi and C++Builder. Some are better than others.

If you want more control than the dbgrid can provide, you will have to do some code to get the data in the stringgrid.
Something like:

Table1.open; //Or Query1, depending what you have..
idx := 0;
while not Table1.eof do
begin
grid1.cells[0, idx] := Table1.Fields[0].asString; //access field by index
grid1.cells[1, idx] := Table1.FieldByName('someField').asString; //access field by name
Table1.next;
idx := idx + 1;
end;


It really depends on what you want to acheive.. I am just guessing but I think you are fairly new to Delphi/C++builder so the quickest way to acomplish a grid with data is using a db-grid.

Good luck

Jens Fudge

ads32w
Posts: 2
Joined: Wed 09 Mar 2011 21:24

Post by ads32w » Thu 10 Mar 2011 12:55

1. I want to load first column to StringGrid.
2. Manually write data to second and third column.
3. Send the whole StringGrid to another table.

Everything is OK but records are only loaded to the StringGrid default columns. In table is 23 records but in StringGrid I can see only 4.

Thank you for help.

AndreyZ

Post by AndreyZ » Thu 10 Mar 2011 15:17

Here is a code example of loading data from the TMSQuery component to the TStringGrid component:

Code: Select all

procedure TMainForm.BitBtnClick(Sender: TObject);
var
  i: integer;
begin
  MSQuery.Open;
  StringGrid.ColCount := MSQuery.FieldCount + 2; // two columns for additional data
  StringGrid.RowCount := MSQuery.RecordCount + 1;
  for i := 0 to MSQuery.FieldCount - 1 do
    StringGrid.Cells[i, 0] := MSQuery.Fields[i].FieldName;
  MSQuery.First;
  while not MSQuery.Eof do begin
    for i := 0 to MSQuery.FieldCount - 1 do
      StringGrid.Cells[i, MSQuery.RecNo] := MSQuery.Fields[i].AsString;
    MSQuery.Next;
  end;
end;
Also you can use the TVirtualTable and TCRBatchMove components for your task. Here is a code example:

Code: Select all

procedure TMainForm.BitBtn1Click(Sender: TObject);
var
  i: integer;
begin
  MSQuery1.SQL.Text := 'select * from table1';
  MSQuery1.Open;
  for i := 0 to MSQuery1.FieldCount - 1 do
    VirtualTable.AddField(MSQuery1.Fields[i].FieldName, MSQuery1.Fields[i].DataType, MSQuery1.Fields[i].Size, MSQuery1.Fields[i].Required);
  CRBatchMove.Source := MSQuery1;
  CRBatchMove.Destination := VirtualTable;
  VirtualTable.Open;
  CRBatchMove.Execute;
  VirtualTable.AddField('test1', ftString, 50); // additional columns
  VirtualTable.AddField('test2', ftString, 50);
  // adding data in additional columns
end;

procedure TMainForm.BitBtn2Click(Sender: TObject);
begin
  MSQuery2.SQL.Text := 'select * from table2';
  CRBatchMove.Source := VirtualTable;
  CRBatchMove.Destination := MSQuery2;
  CRBatchMove.Execute;
end;
Note that in this case table2 must have the exact fields that table1 has, and additional fields test1 and test2. For more information about the TVirtualTable and TCRBatchMove components, please read the SDAC documentation.

Post Reply