ListView Insert New Column

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
ST3VOAsked:
Who is Participating?
 
xr1140Connect With a Mentor Commented:
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
 
mokuleCommented:
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
 
xr1140Commented:


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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ST3VOAuthor Commented:
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
 
xr1140Commented:

at point 4 you are loosing me .... you are trying to insert a column (vertical) as a row (horizontal) ?!?
0
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
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
 
xr1140Commented:

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
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
Still have problems with the numbers thou :o/
0
 
xr1140Commented:

i think because your items have different numbers of subitems.
0
 
ST3VOAuthor Commented:
I'll check!
0
 
xr1140Commented:

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
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
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
 
xr1140Commented:

[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
 
ST3VOAuthor Commented:
Thanks for your help :o)

0
 
xr1140Commented:


you are welcome, but why a B grade ?
0
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
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
 
xr1140Commented:

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
 
ST3VOAuthor Commented:
OK Thanks !!!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.