Grab Data from DBGrid

eNarc
eNarc used Ask the Experts™
on
Hi, I'd like to grab data from a dbgrid

how do I do this if I can do this with listview, how can I do it with dbgrid

RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[0]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[1]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[2]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[3]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[4]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[5]);
RichEdit1.Lines.Add(ListView1.Items.Item[3].SubItems[6]);

this will grab the data from the selected columns from the selected row, so how can I do this with dbgrid?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Your dbgrid has a datasource property, that datasource has a dataset property, that dataset is what u should reference to retrieve records or field values. Practically all datasets are pointing to only one record at a time, and u can call first, next, previous, or last to navigate between available records. Once on your desired record, you can access field values like this:
Mydataset.fieldbyname('FieldName').asstring
Which returns the value as a string
Hope this helps

Author

Commented:
how do I put the data from the dataset into listview without it putting it into dbgrid? it just goes into listview?

Commented:
As addition to Rtwolf

begin
Mydataset.First;
  while not Mydataset.EOF do
  begin
  RichEdit1.Lines.Add(Mydataset.fieldbyname('FieldName1').asstring);
  RichEdit1.Lines.Add(Mydataset.fieldbyname('FieldName2').asstring);
  RichEdit1.Lines.Add(Mydataset.fieldbyname('FieldName3').asstring);
  Mydataset.Next;
 end;
end;

Commented:
use Fields index of ADOQuery1

RichEdit1.Lines.Add(ADOQuery1.Fields[3].AsString );

try the following code

procedure TForm1.Button1Click(Sender: TObject);
begin
 ADOQuery1.First;
 while not ADOQuery1.Eof do
 begin
 RichEdit1.Lines.Add(ADOQuery1.Fields[3].AsString );
 ADOQuery1.Next;
 Application.ProcessMessages;
 end;
end;

Open in new window

Author

Commented:
i can't seem accept the 2 comments.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial