How to copy selected fields from DBGrid to clipboard lazarus / 16-Databases ---------------------------------------------------------------------- tjpren · 2012-04-13 08:01 hello, I was trying to add functionality to my DB viewer, and add the ability to copy selected fields from the DBGrid to the clipboard (or via a memo object to the clipboard). I'm running at a loss to see how to do it. I suspect that it has something to do with the bookmark property Code: [Select]procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);   var   i: Integer;   sum : Single; begin   if DBGrid1.SelectedRows.Count > 0 then   begin     sum := 0;     with DBGrid1.DataSource.DataSet do     begin       for i := 0 to DBGrid1.SelectedRows.Count-1 do       begin         GotoBookmark(Pointer(DBGrid1.SelectedRows.Items[i]));         memo1.text:=TBookmarklist.Items[i];       end;     end;   end end;      This doesn't work - has anyone got any ideas? Regards ---------------------------------------------------------------------- Mando · 2012-04-13 10:29 ds.GotoBookmark(Tbookmark(aRejilla.SelectedRows));     result:=result+','+quotedStr(ds.fields[0].asString);  Code: [Select]procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);   var   i: Integer;   sum : Single; begin   if DBGrid1.SelectedRows.Count > 0 then   begin     sum := 0;     with DBGrid1.DataSource.DataSet do     begin       for i := 0 to DBGrid1.SelectedRows.Count-1 do       begin                 GotoBookmark(Tbookmark(DBGrid1.SelectedRows[i]));         memo1.lines.add(format('%s:%s',[DBGrid1.columns[0].title.capti on,DbGrid1.columns[0].field.asString)]);       end;     end;   end end;      This adds first column title and field value to memo1 regards ---------------------------------------------------------------------- tjpren · 2012-04-13 11:22 Mando, Thanks for this - I can see this is a bit more complicated than I thought. Your example gives me the contents of column 0 of the selected rows.  I'd have to loop round to pick up the other columns. The output would be a continuous string. I can live with that, although I was looking for something that would match the grid ie Caption1, Caption2, Caption3,...Caption n data,        data,        data,..       data data,        data,        data,...      data Any further thoughts. By the way, you were most helpful.  I can at least copy data to the clipboard. ---------------------------------------------------------------------- tjpren · 2012-04-13 13:11 Hello, I think I missed some of your explanation before: Code: [Select]result:=result+','+quotedStr(ds.fields[0].asString);  I searched through the forum and found Topic: DbGrid to CSV. When I looked closely, it had a similar format, and I adjusted it slightly to output to a memo field.  I see that by iterating through the column count, and reading the fields back on itself, I essentially get a full row.  Unfortunately I just wasn't quite aware of what Mando was showing me. Code: [Select]procedure TForm1.Button3Click(Sender: TObject); const   //Tab character   Delim = CHR(9); var   S: String;   I: Integer;   //Stream: TFileStream; begin   //Stream := TFileStream.Create('C:\temp\test.txt', fmCreate);   while not DBGrid1.DataSource.DataSet.EOF do   begin     S := '';     for I := 0 to DBGrid1.Columns.Count - 1 do     S := S + TColumn(DBGrid1.Columns[I]).Field.AsString + Delim;     //line end character     S := S + CHR(13);     //Stream.Write(PChar(S)^, Length(S));     Memo1.Lines.Add(S);     DBGrid1.DataSource.DataSet.Next();   end;   //Stream.Free();   Memo1.SelectAll;   Memo1.CopyToClipboard; end;            This code seems to work OK, so I can now select rows (having multiselect set to true), and copy the slection to a clipboard to paste to a spreadsheet, or a notepad etc. ---------------------------------------------------------------------- Mando · 2012-04-13 15:34 Hello: But your code exports full table, not only selected records. regards. ---------------------------------------------------------------------- tjpren · 2012-04-14 00:19 Mando, Oddly enough, if I mouse select, say row 3, then all rows from row 3 to the end of the table are copied. If I select row 1, the entire table is copied. I don't know why. Maybe, by default with multiselect on, the dataset is defined from the row of the mouse select? Regards ---------------------------------------------------------------------- tjpren · 2012-04-14 10:07 Hi, Think I might have nailed it now  .  This allows any selection of a DBGrid only, to be copied to a clipboard.  Any rows selected, should now be copied, rather than the whole table. Again, I thank Mando for his code snippet that put me on the correct path. Code: [Select]procedure TForm1.Button3Click(Sender: TObject); Const   //Tab character   Delim = CHR(9); var   i: Integer;   x: Integer;   S: String;   Y: String; begin   //Copy Fieldnames First   Y := '';   for x := 0 to DBGrid1.Columns.Count - 1 do   Y := Y + (format('%s',[DBGrid1.columns[x].title.caption]))+ Delim;   Memo1.Lines.Add(Y);   //Copy Field contents   if DBGrid1.SelectedRows.Count > 0 then   begin     with DBGrid1.DataSource.DataSet do     begin       for i := 0 to DBGrid1.SelectedRows.Count-1 do       begin           //Bookmark sets the position of the selection           S := '';           GotoBookmark(Tbookmark(DBGrid1.SelectedRows[i]));           for x := 0 to DBGrid1.Columns.Count - 1 do           Begin           S := S + (format('%s',[DbGrid1.columns[x].field.asString]))+ Delim;           end;           Memo1.Lines.Add(S);         end;       end;     end;   //Copy to a memo field, then to the clipboard   Memo1.SelectAll;   Memo1.CopyToClipboard; end;