We help IT Professionals succeed at work.

How to add in listview

QC20N
QC20N asked
on
Medium Priority
441 Views
Last Modified: 2013-11-23
I have 3 columns and I want to add text into them.

How do I do that?
Comment
Watch Question

ThievingSixDeveloper
CERTIFIED EXPERT

Commented:

  ListView1.ViewStyle := vsReport;
  With ListView1.Items.Add Do
    begin
    Caption := 'Column 1';
    SubItems.Add('Column 2');
    SubItems.Add('Column 3');
  end;

Open in new window

Author

Commented:
And if I have more than 1 row. How do I add text to the next row?
ThievingSixDeveloper
CERTIFIED EXPERT

Commented:

  ListView1.ViewStyle := vsReport;
  With ListView1.Items.Add Do
    begin
    Caption := 'Row 1 Column 1';
    SubItems.Add('Row 1 Column 2');
    SubItems.Add('Row 1 Column 3');
  end;
  With ListView1.Items.Add Do
    begin
    Caption := 'Row 2 Column 1';
    SubItems.Add('Row 2 Column 2');
    SubItems.Add('Row 2 Column 3');
  end;

Open in new window

Author

Commented:
But I have a query an I don't know what the result will be. My query search in an DB for a name. So the name could apear 2 , 3 or 40 times in my DB. So I can't use that solution.  

Author

Commented:
And btw, if I do what you have written. I get the output in the attach file.
            while not ADOQuery1.Eof do
            begin
              Listview1.Items.Add.Caption := ADOQuery1.FieldByName('cn').AsString;
              Listview1.Items.Add.SubItems.Add(ADOQuery1.FieldByName('samAccountName').AsString);
              Listview1.Items.Add.SubItems.Add(copy(ADOQuery1.FieldByName('manager').AsString,4,pos(',',ADOQuery1.FieldByName('manager').AsString)-4));
              ADOQuery1.Next;
            end;

Open in new window

untitled2.JPG
Developer
CERTIFIED EXPERT
Commented:
Yea, you implemented it slightly wrong.

Here's a corrected version:
            while not ADOQuery1.Eof do
            begin
              With ListView1.Items.Add Do
                begin
                Caption := ADOQuery1.FieldByName('cn').AsString;
                SubItems.Add(ADOQuery1.FieldByName('samAccountName').AsString);
                SubItems.Add(copy(ADOQuery1.FieldByName('manager').AsString,4,pos(',',ADOQuery1.FieldByName('manager').AsString)-4));
              end;
              ADOQuery1.Next;
            end;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Wow. I didn't know there was a different between what you did and I did. But the last Subitems.Add overwrite the 1st Subitems.Add. There is notthing in the 3rd Column.

Author

Commented:
Please, you have any input to this?

Author

Commented:
Tried to add 1 more column and added 1 more SubItem.add. Now it works.

Author

Commented:
Btw, how do I avoid that you can't edit the columns?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.