Results 1 to 6 of 6

Thread: [RESOLVED] How to add subitems in ListView

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Resolved [RESOLVED] How to add subitems in ListView

    hi I am trying to add to the sub items in a listview but am getting an error, I add a listview and added two columns here the code i am using below.

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                // add some items
                for (int x = 0; x < 10; x++)
                {
                    //Add to the first colum
                    listView1.Items.Add("Item " + x.ToString());
                    //Add to the next colum 1 gives an error
                    listView1.Items[listView1.Items.Count - 1].SubItems[1].Text = "SubItem";
                }
            }

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to add subitems in ListView

    Try someting like this instead:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
    	listView1.View = View.Details;
    	listView1.Columns.Add("Item");
    	listView1.Columns.Add("SubItem 1");
    	listView1.Columns.Add("SubItem 2");
    
    	for (int i = 0; i <= 9; i++)
    	{
    		listView1.Items.Add("Item" + (i + 1));
    		listView1.Items[i].SubItems.Add("Sub Item 1");
    		listView1.Items[i].SubItems.Add("Sub Item 2");
    	}
    }

  3. #3
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: How to add subitems in ListView

    Quote Originally Posted by BenJones View Post
    hi I am trying to add to the sub items in a listview but am getting an error, I add a listview and added two columns here the code i am using below.

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                // add some items
                for (int x = 0; x < 10; x++)
                {
                    //Add to the first colum
                    listView1.Items.Add("Item " + x.ToString());
                    //Add to the next colum 1 gives an error
                    listView1.Items[listView1.Items.Count - 1].SubItems[1].Text = "SubItem";
                }
            }

    hey,
    your problem in
    Code:
    SubItems[1]
    use
    Code:
    SubItems[0]
    if you let us know what you try to do we can help.
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to add subitems in ListView

    The Items.Add method returns the item, so you can use that to add subitems to. If you only want to add one subitem:
    csharp Code:
    1. myListView.Items.Add("item text").SubItems.Add("subitem text");
    If you're adding multiple subitems:
    csharp Code:
    1. ListViewItem item = myListView.Items.Add("item text");
    2.  
    3. item.SubItems.Add("subitem text");

  5. #5

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: How to add subitems in ListView

    Thanks RhinoBull that seems to work for me, thanks for the rest of the help to from the others.

  6. #6
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] How to add subitems in ListView

    Glad to hear it
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width