tags:

views:

652

answers:

4

I create a listview and the number of columns are determined at runtime. I have been reading texts on the web all over about listview( and I still am) but I wish to know how to add items to a specific column in listview I thought something like:

m_listview.Items.Add("1850").SubItems.Add("yes");

would work assuming the "1850" which is the text of the column would be the target column.

A: 

Something like this maybe?

ListViewItem item = m_listview.Items.Add("1850");
item.Subitems.Add(string.Empty); //Make an add method that does this for every column you got and then you can use the indexer below.
item.Subitems[1].Text = "Test";

Index 0 is the main item. Index 1 > are the subitems.

Jesper Palm
your code doesn't work
Dark Star1
dark star, you need to switch to report view and insert the second column first (lv.Columns.Add, I guess). Note that a Listview considers an entire line in report view as a single "Item", the functionality for sub items is limited.
peterchen
Damn that's ugly... There must be a better solution.
Jesper Palm
+2  A: 

I'm usially inherit ListViewItem as :

public class MyOwnListViewItem : ListViewItem 
{
    private UserData userData;

    public MyOwnListViewItem(UserData userData) 
    {
        this.userData = userData;
        Update();
    }

    public void Update() 
    { 
       this.SubItems.Clear(); 
       this.Text = userData.Name; //for first detailed column

       this.SubItems.Add(new ListViewSubItem(this, userData.Surname)); //for second can be more
    }
}

where

public class UserData 
{
   public string Name;
   public string Surname;
}

using

listView1.Items.Add(new MyOwnListViewItem(new UserData(){Name="Name", Surname = "Surname"}));
Chernikov
Wow, very nice implementation. +1
Nazadus
A: 
ListViewItem item = new ListViewItem("foo");
item.SubItems.Add("foo2");
this.listView1.Items.Add(item);

ListViews don't support databinding and strongly typed naming. I would recommend considering using a DataGridView instead. Depending on what you need, it may save yourself some sanity points.

Chernikov has it nicely implemented to make things a bit saner.

Nazadus
+4  A: 

ListviewItems aren't aware of your listview columns.

In order to directly reference the column, you first need to add all the columns to the listviewItem.

So... lets say your ListView has three columns A,B & C this next bit of code will only add data to column a:

ListViewItem item = new ListViewItem();
item.text = "Column A";

m_listView.Items.Add(item);

To get it to add text to column C as well, we first need to tell it it has a column B...

ListViewItem item = new ListViewItem();
item.text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");

m_listView.Items.Add(item);

So now we'll have columns A,B & C in the listviewItem, and text appearing in the A and & Columns, but not the B.

Finally.. now we HAVE told it it has three columns, we can do whatever we like to those specific columns...

ListViewItem item = new ListViewItem();
item.text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");

m_listView.Items.Add(item);

m_listView.Items[0].SubItems[2].Text  = "change text of column C";
m_listView.Items[0].SubItems[1].Text  = "change text of column B";
m_listView.Items[0].SubItems[0].Text  = "change text of column A";

hope that helps!

Sk93
Thank you very much SK93 for explaining it so that a noob like me can understand and Chernikov for the idea.
Dark Star1
hehe you're welcome... we were all noobs once :)
Sk93