Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copying Data From DBGrid to ListView

Posted on 2007-10-05
3
Medium Priority
?
750 Views
Last Modified: 2010-05-18
Hi all,

Is it possible to copy all contents (Including Headers) from a DBGrid to a ListView?

If so how?

Thanks

ST3VO
0
Comment
Question by:ST3VO
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 2000 total points
ID: 20020987
Well assuming there is a query attached to the grid, loop through the record set

var
   i: smallInt;
   slFields: TStringList;
   lsti: TListItem;
   lci: TListColumn;
begin
   DBGrid1.DataSource.DataSet.DisableControls;
   try
      slFields := TStringList.Create;
      try
         DBGrid1.DataSource.DataSet.GetFieldNames( slFields);
         // add required number of columns
         for i := 0 to slFields.Count - 1 do
         begin
            lci := ListView1.Columns.Add;
            lci.Caption := slFields.Strings[ i];
         end;

         // now add the data
         DBGrid1.DataSource.DataSet.first;
         while not DBGrid1.DataSource.DataSet.Eof do
         begin
            lsti := ListView1.Items.Add;
            for i := 0 to slFields.Count - 1 do
            begin
               if i = 0 then
                  lsti.Caption := DBGrid1.DataSource.DataSet.fieldByName( slFields.strings[ i]).AsString
               else
                  lsti.SubItems.Add( DBGrid1.DataSource.DataSet.fieldByName( slFields.strings[ i]).AsString);
            end;

            DBGrid1.DataSource.DataSet.Next;
         end;
      finally
         freeAndNil( slFields);
      end;
   finally
      DBGrid1.DataSource.DataSet.first;
      DBGrid1.DataSource.DataSet.EnableControls;
   end;

you can also replace DBGrid1.DataSource.DataSet with the name of the query
0
 

Author Comment

by:ST3VO
ID: 20021023
Wow..PERFECT!!! :o)

Thanks a million!

p's: Mike I know that you know about TMS Components and I've got a couple of questions opened on that.
I just thought I'd let you know in case you wanted to have a look at them.

Anyway, that worked GREAT!!!

Cheers

ST3VO
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 20021098
Sure I'll take a quick look, was just about to comment on the AdvDBGrid one you have open.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question