-
May 24th, 2010, 04:19 PM
#1
Thread Starter
Fanatic Member
[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";
}
}
-
May 24th, 2010, 04:44 PM
#2
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");
}
}
-
May 26th, 2010, 04:50 AM
#3
Frenzied Member
Re: How to add subitems in ListView
Originally Posted by BenJones
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
use
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
-
May 26th, 2010, 05:29 AM
#4
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:
myListView.Items.Add("item text").SubItems.Add("subitem text");
If you're adding multiple subitems:
csharp Code:
ListViewItem item = myListView.Items.Add("item text"); item.SubItems.Add("subitem text");
-
May 28th, 2010, 04:02 AM
#5
Thread Starter
Fanatic Member
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.
-
May 28th, 2010, 04:46 AM
#6
Frenzied Member
Re: [RESOLVED] How to add subitems in ListView
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|