?
Solved

ListView Insert New Column

Posted on 2007-10-01
22
Medium Priority
?
453 Views
Last Modified: 2010-04-05
Hi all,

I need to Insert a new Column to a ListView...Delete the First Row and Replace with a new one.

I've Tried:

 ListView1.Columns.Delete(0); //Delete the first Column

 ListView1.Columns.Insert(0); //Insert new column at position 0

  Once that is done I want to populate the New column with numbers "1 to end of list count"

I've tried a loop but the numbers are being populated on the second Column and not with the new one.

Anyone have any ideas please?

Thanks

ST3VO
0
Comment
Question by:ST3VO
  • 12
  • 9
22 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 19994980
Is there any sense to delete then insert?
I can't see any.
Just populate

  for i := 0 to listView1.Items.Count-1 do
    listView1.Items[i].Caption :=intToStr(i+1);
0
 
LVL 5

Expert Comment

by:xr1140
ID: 19995116


When you work with columns you have to deal with subitems. By default the item is created on column with index 0 and every subitem in the other columns. If you delete de column 0 then the item will be in the former column 1 (now with index 0). The column you insert in position 0 will be populated with the last subitem of the item.

hope it make sense to you.




uMain.dfm
===============================


object Form1: TForm1
  Left = 423
  Top = 172
  Width = 461
  Height = 481
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 175
    Top = 10
    Width = 266
    Height = 431
    Columns = <>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Button2: TButton
    Left = 10
    Top = 365
    Width = 156
    Height = 26
    Caption = 'remove &&  insert'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button1: TButton
    Left = 10
    Top = 415
    Width = 156
    Height = 26
    Caption = 'Add items'
    TabOrder = 2
    OnClick = Button1Click
  end
end














uMain.pas
===============================


unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button2: TButton;
    Button1: TButton;
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
  newCol: TListColumn;
begin
  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);

  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);

  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  newCol, oldCol : TListColumn;
  i : integer;
  existItem: TListItem;
begin
  oldCol := ListView1.Columns.Items[0];
  oldCol.Destroy;

  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col new';
  newCol.Index := 0;

  for i := 0 to (ListView1.Items.Count-1) do begin

    existItem := ListView1.Items[i];
    // the last subitem (b) will be in the newly inserted column
    existItem.SubItems.Strings[1] := IntToStr( ListView1.Items[i].Index )
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  newItem: TListItem;
begin
  newItem := ListView1.Items.Add;
  newItem.Caption := 'Item ' + IntToStr(ListView1.Items.Count);
  newItem.SubItems.Add('a');
  newItem.SubItems.Add('b');

end;

end.
0
 

Author Comment

by:ST3VO
ID: 19997014
OK ... I'll explain further!

THese are the steps on what I've trying to do:

1. Populate ListView ....(DONE)
2. Check if there is a column called ID ...(DONE)
3. If it exists then Populate it with numbers from 1 to Count Increment.
4. If not found insert one as the first row and add the items numbers 1 to count Increment.

Hope this help!!

Thanks

ST3VO
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:xr1140
ID: 19997182

at point 4 you are loosing me .... you are trying to insert a column (vertical) as a row (horizontal) ?!?
0
 

Author Comment

by:ST3VO
ID: 19997271
Just a new Column

For Example:

ID  <--Column Header Name
1  <--- Data  from 1 onwards
2
3
4
5
6
7
8
etc...

Hope this helps!

0
 
LVL 5

Accepted Solution

by:
xr1140 earned 1500 total points
ID: 19997423
try this, i`ve inserted some comments in there.



uMain.dfm
===============================





object Form1: TForm1
  Left = 685
  Top = 292
  Width = 582
  Height = 481
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 175
    Top = 10
    Width = 386
    Height = 431
    Columns = <>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object butChange: TButton
    Left = 10
    Top = 365
    Width = 156
    Height = 26
    Caption = 'remove &&  insert'
    TabOrder = 1
    OnClick = butChangeClick
  end
  object butAdd: TButton
    Left = 10
    Top = 415
    Width = 156
    Height = 26
    Caption = 'Add items'
    TabOrder = 2
    OnClick = butAddClick
  end
end












uMain.pas
===============================



unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    butChange: TButton;
    butAdd: TButton;
    procedure FormShow(Sender: TObject);
    procedure butChangeClick(Sender: TObject);
    procedure butAddClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
      newCol: TListColumn;
      i : integer;
begin

      // add some columns in there
      for i:=0 to 3 do begin
            newCol := ListView1.Columns.Add;
            newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);
      end;

      //add a dummy ID column
      newCol := ListView1.Columns.Add;
      newCol.Caption := 'ID';
end;

procedure TForm1.butChangeClick(Sender: TObject);
var
  newCol, oldCol : TListColumn;
      subItemsCount, colIndex, i, j : integer;
      existItem: TListItem;

begin
      colIndex := -1;

      //cycle columns
      for j := 0 to (ListView1.Columns.Count-1) do begin
            //is it exist save it index in colIndex
            if ListView1.Columns[j].Caption = 'ID' then colIndex := j;
      end;

      // if column ID has been found
      if colIndex <> -1 then begin

            oldCol := ListView1.Columns[colIndex];
            oldCol.Destroy;
            {
                  if you have already have a ID column the easyest way is to delete it and
                  create a new one at index 0, because if you only try to change de index of
                  the column then the content will not change/shift accordinglly to you columns
            }
            newCol := ListView1.Columns.Add;
            newCol.Caption := 'ID';
            newCol.Index := 0;
            {
                  adding a new column will preserve your previous item - column relatioship
            }
      end;


      //cycle items
      for i := 0 to (ListView1.Items.Count-1) do begin
            //find the current item in the list
            existItem := ListView1.Items[i];
            {
                  add a new subitem to it just to hold you ID, by default will be placed in
                  the last column created (ID) =>  last subItem is in the last column created
            }
            existItem.SubItems.Add( IntToStr(ListView1.Items[i].Index) )
      end;

end;

procedure TForm1.butAddClick(Sender: TObject);
var
      newItem: TListItem;
begin
      // populate the list
      newItem := ListView1.Items.Add;
      newItem.Caption := 'Item ' + IntToStr(ListView1.Items.Count);
      newItem.SubItems.Add('subitem a');
      newItem.SubItems.Add('subitem b');
      newItem.SubItems.Add('subitem c');
end;

end.
0
 

Author Comment

by:ST3VO
ID: 19997472
I'm having some problems with the numbers here.

I'm gettin something like this:

ID
1
2
3
4
5
6
7
8

10
11

13


16

etc..

Any ideas why?

0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997474

i`m presuming you want you ID column to be first, that`s way i`ve done it like that. if you already have a ID column and just wanted to preserve it`s index and fill it it`s even easier
0
 

Author Comment

by:ST3VO
ID: 19997489
I've modified it a bit so I don't have to move the column and then copy it to index pos 0.

New code:

var
      newCol: TListColumn;
      i : integer;
begin

      //add a dummy ID column
      newCol := ListView1.Columns.Add;
      newCol.Index:=0; //Position the Column where I need it.
      newCol.Caption := 'ID';
end;

0
 

Author Comment

by:ST3VO
ID: 19997498
Still have problems with the numbers thou :o/
0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997505

i think because your items have different numbers of subitems.
0
 

Author Comment

by:ST3VO
ID: 19997521
I'll check!
0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997590

if you make you ID column index 0 from the start then you item will reside in that column and when you trying to populate that column ( ID column) with indexes according to the colIndex you found your subitem with that index will be replaced.


my advice ...
1. populate the list without a ID column
2. when done add the ID column and fill it up

in this way you have a better control
0
 

Author Comment

by:ST3VO
ID: 19997639
Well that what I'm going to do.

I'm just going to add the ID Column before saving it back to CSV.

I've checked the CSV file and there are blank fields. I wonder it this is coursing the problem with the numbers :o/
0
 

Author Comment

by:ST3VO
ID: 19997662
This is what I've broken the code downto:

New Code:

procedure TForm1.Button2Click(Sender: TObject);
var
      newCol: TListColumn;
      z,j : integer;
      existItem: TListItem;
begin

      j:=0;

        //if ID Column already exists then exit
       if ListView1.Columns.Items[j].Caption =('ID') then exit;

       //add a dummy ID column
      newCol := ListView1.Columns.Add;
      newCol.Index:=0;
      newCol.Caption := 'ID';

      //Now insert the numbers

      for z := 0 to (ListView1.Items.Count-1) do begin
            //find the current item in the list
            existItem := ListView1.Items[z];
         
            existItem.SubItems.Add( IntToStr(ListView1.Items[z].Index) )

end;
end;

I just need to sort out the problems with the numbers and I'm sorted :o)

0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997681

[quote]
i've checked the CSV file and there are blank fields. I wonder it this is coursing the problem with the numbers [/quote]

i belive that`s the cause



i thought that you have and don`t know where the ID column is in your data and i`ve tried to provide a general example ... but if you don`t have such constraint then choose the simple way, build your own :D
0
 

Author Comment

by:ST3VO
ID: 19997717
Thanks for your help :o)

0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997777


you are welcome, but why a B grade ?
0
 

Author Comment

by:ST3VO
ID: 19997783
I last question please! The 1st field it not been populated by the numbers on the column it. It's starting on the second field down.

Do you have any ideas why?
0
 

Author Comment

by:ST3VO
ID: 19997836
It's OK! I fixed it now:

Modified this line:

existItem.SubItems.Add( IntToStr(ListView1.Items[z].Index+1<--HERE) )

Thanks a lot!!!

ST3VO
0
 
LVL 5

Expert Comment

by:xr1140
ID: 19997855

make sure that your item has the same number or subitems ... the subitem you add must have the same index for every item ( having the same index means that it will be shown in the same column for every item)
0
 

Author Comment

by:ST3VO
ID: 19997899
OK Thanks !!!

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 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