How do I add to a specific column in Listview item?

49,631

Solution 1

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 and 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 and C in the ListViewItem, and text appearing in the A and C columns, but not the B.

Finally... now that 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!

Solution 2

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"}));

Solution 3

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.

Solution 4

This is what I do because the columns are added at run time via the users preference.

I am using a listView to display information from a database to the user. ( I probably should use DataGrid, but when I first made the program DataGrid didn't have built in full row select and I was too big of a newb to go through the custom control examples I found)

The user selects which columns they want to show up out of the total, how wide, and the title text.

I created a user preferences table that saved information about the columns to add

  • ColumnVisible = bool
  • ColumnText = string = text to display in header
  • columnWidth = int
  • ColumName = string = the exact name this column will refer to in my other database
  • ColumnIndex = int = the display index of the column

I also made a class ColumnPreferences and added a property for each of those columns

Used DataReader to make List<ColumnPreferences>

I sort this list according to display index, so I can iterate through it in the order the columns are displayed.

I do an if statement checking if the next preference in preferences is visible.

If it is then I add the column using ListView1.Columns.Add(ColumnName, ColumnText, ColumnWidth);

At this point the columns are all set, to save where the user drags them around to I do basically the opposite. I iterate through all column in Listview.columns and save the column display index and column width back into my user preference table

When loading the actual data from the table I use this same list of column preferences to check whether I should display the data.

it is again sorted by display index, I check if it is visible, if it is not I skip it and go to the next one

if it is visible I use myDataReader[ ColumnPreference.ColumnName ] to look up the approprate ordinal for the data I want.

I just add each of those results, in order, to a list view item / subitems then add them to the list view

Share:
49,631
Dark Star1
Author by

Dark Star1

The true mind can weather all the lies and illusions without being lost. The true heart can toughen the poison of hatred without being harmed. Since beginning-less time, darkness thrives in the void, but always yields to purifying light.

Updated on July 05, 2022

Comments

  • Dark Star1
    Dark Star1 almost 2 years

    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.

  • peterchen
    peterchen over 14 years
    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.
  • Theo
    Theo over 14 years
    Wow, very nice implementation. +1
  • Jesper Palm
    Jesper Palm over 14 years
    Damn that's ugly... There must be a better solution.
  • neo
    neo almost 12 years
    Great answer! thanks! Amazing quote from Sk93, "We were all noob once", Very inspiring and motivating! :)