Tour a StringGrid and TDBGrid

1

Dear, I have a problem, I am new to delphi and I want to go through a StirngGrid also a TDBGrid, I would like some suggestion. I am developing a client - server application (DataSnap), but I am not able to reccure the STringGrid, I want to develop my own menu to go through the Grid. I've been stuck in this for two days now.

Edited ..

Now I have this problem ..

    procedure TForm5.SpeedButton3Click(Sender: TObject);
var I : integer;
var J : integer;
var Str : String;
var client : TTServerMethodsClient;
begin
client :=   TTServerMethodsClient.Create(SQLConProductosGrid.DBXConnection);

 // Recorrido por las filas
  for I := 0 to (StringGridBindSourceDB1.RowCount - 1) do begin

    // Recorrido por las columnas
    for J := 0 to (StringGridBindSourceDB1.ColCount - 1) do begin

      // Acceso a cada celda
      Str := StringGridBindSourceDB1.Cells[J,I];
      showMessage('Acceso a la celda :'+Str);
    end;
  end;



end;
procedure TForm5.StringGridBindSourceDB1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
showMessage('Acceso a la celda');
BindSourceDB1.DataSet.Open;
BindSourceDB1.DataSet.DisableControls;
 BindSourceDB1.DataSet.Edit;
 BindSourceDB1.DataSet.Post;
 BindSourceDB1.DataSet.EnableControls;
end;

When I want to access the cell in the TStrinGrid it tells me this error:

Cannot Modify a Real-Only DataSet
    
asked by Nahuel Jakobson 10.05.2018 в 23:04
source

1 answer

2

The TDBGrid are components that are "linked" to a data source, so the normal thing in that case is to traverse the data source, not the component. Either a TTable , TQuery or any of the components derived from TDataset that would be the origin to all of them. A simple example could be this:

var
  ds:TDataset;
begin
  // Asocia la variable
  ds := DBGrid1.DataSource.DataSet;
  // Ir al inicio
  ds.First;
  // recorrer el Dataset asociado a un TDBGrid
  while (not ds.Eof) do begin
    // ...
    str := ds.FieldByName('Nombre').AsString;
    // ...
    ds.Next;
  end;

The case of TStringGrid is different, because it is the component itself that stores the data, so in this case, you must go through the rows and columns to obtain them. You can review the properties in the help:

  • Rows : Access to rows
  • Cols : Access to columns
  • Cells : Access to cells

( List of TStringGrid properties )

  • RowCount and ColCount : Return the number of rows and columns defined.

A simple example of accessing the cells of a TStringGrid would be this:

  // Recorrido por las filas
  for I := 0 to (StringGrid1.RowCount - 1) do begin
    // Recorrido por las columnas
    for J := 0 to (StringGrid1.ColCount - 1) do begin
      // Acceso a cada celda
      Str := StringGrid1.Cells[J, I];
    end;
  end;
    
answered by 11.05.2018 в 12:42