-2

Is there any way to show a selected DBGrid record record in a memo? I tried this type of code but this shows the titles of the columns and I want to show the fields' text.

    procedure TForm6.btnShowClick(Sender: TObject);
    var
      l: String;
      p: Integer;
      i: Integer;
      m: String;
    begin
       m:='';

       for p := 0 to dbGrid1.Columns.Count - 1 do
         m:=m+(format('%s',[dbGrid1.Columns[p].Title.Caption]))+lm;
         Memo1.Lines.Add(m);

        if Dbgrid1.SelectedRows.Count>0 then
        begin
          with dbgrid1.DataSource.DataSet do
          begin
            l:=' ';
            GoToBookMark(tBookmark(dbGrid1.SelectedRows[i]));

            for p := 0 to Dbgrid1.Columns.Count - 1 do
            begin
              l:=l+(format('%s',[dbgrid1.Columns[p].Field.AsString]));
            end;

            Memo1.Lines.Add(l) ;
          end;
        end;        
      end;
    end.

I tried to change the [dbgrid1.Columns[p].Field.AsString] so that it uses the text of the field but it doesn't work. Note-This is the template code that i hope to use

Is there an easier way without Sql to simply show a selected record in a memo?

Michael
  • 41,989
  • 11
  • 82
  • 128
L. Blue
  • 1
  • 3
  • 2
    Sorry, I can't make sense of your question. Firstly, dbgrid1.Columns[p].Field.AsString] **does** return the value in the column cell, not its title, and secondly, what has Sql got to do with what you are asking? – MartynA Mar 29 '16 at 10:14
  • Are you aware of the difference between the _current row_ and _selected rows_? The `SelectedRows` property retruns the list of rows that are marked as _selected_. That is not the same as the _current row_, which correspond to the current record of the underlying dataset. I am unsure if you really want `SelectedRows` here. – Uwe Raabe Mar 29 '16 at 11:41
  • You don't seem to set the `i` index of `GoToBookMark(tBookmark(dbGrid1.SelectedRows[i]));` anywhere. – Tom Brunberg Mar 29 '16 at 13:39
  • Have you debugged SelectedRows.Count to make sure you have a row selected? Looks like you add title captions regardless of whether or not a row is selected... – John Easley Mar 30 '16 at 00:44
  • check the indentation of your code, you do know that only this line m:=m+(format('%s',[dbGrid1.Columns[p].Title.Caption]))+lm; will be executed in the loop, all other lines are not – GuidoG Mar 30 '16 at 09:35

1 Answers1

-1

Use a StringGrid instead of a standard grid as they can store string data in their cells[x,y] property. Everything else is much the same code wise.,