2

I have a listView. I want to add data in row number one, column number two. I have tried the following:

listView1.Items[0].Subitems[1].Text = "my data";

I get an error like this '0' is not a valid index. Then i tried this:

if (listViewDownloadList.SelectedItems.Count == -1)
        {
            listViewDownloadList.Items[0].SubItems[1].Text = "Hello";
        }

Now, nothing is happening. No insert, no error.

But it doesn't work. Can you help me out here?

4
  • Does your list view having item at index 1 which you want to update or you want to insert new row? Commented Aug 26, 2017 at 18:12
  • Sorry, there was a mistake in the code. I already have a row. There are six column in the row. Everyone of them has a value without column 2 that is items[0].Subitems[1]. I want to add data in that position. how can i do that? Commented Aug 26, 2017 at 18:28
  • Can you please explain did your list view have column index 1 to which you want to update? Commented Aug 26, 2017 at 18:44
  • yes, there are six column in the row. Everyone of then has data. I want to update the mentioned position. Commented Aug 27, 2017 at 7:15

2 Answers 2

2

You are getting the error '0' is not a valid index because your list view did not have any element. First you need to create a row like below-

 string[] row = { "Hello" };
 var listViewItem = new ListViewItem(row); 

Then you need to add that row into listview like below-

listView1.Items.Add(listViewItem);
Sign up to request clarification or add additional context in comments.

3 Comments

I already have a row. There are six column in the row. Every column has data without column 2. I want to add data in the column two. How can I do that?
Does this work listView1.Items[0].Subitems[1].Add("my data")?
No, doesn't work. Says listViewItem.SubItem doesn't contain a definition for add
0

You can add only subitem to existing item in listView like this:

listView1.Location = new System.Drawing.Point(20, 20);
listView1.Width = 250;

// The View property must be set to Details for the 
// subitems to be visible.
listView1.View = View.Details;

// Each SubItem object requires a column, so add three columns.
this.listView1.Columns.Add("Key", 50, HorizontalAlignment.Left);
this.listView1.Columns.Add("Co1 1", 100, HorizontalAlignment.Left);
this.listView1.Columns.Add("Col 2", 100, HorizontalAlignment.Left);


// Add a ListItem (row1) object to the ListView.
ListViewItem row1 = new ListViewItem();
row1.Text = "Row 1";
row1.SubItems.Add("A");
row1.SubItems.Add("");
listView1.Items.Add(row1);


// Add a ListItem (row2) object to the ListView.
ListViewItem row2 = new ListViewItem();
row2.Text = "Row 2";
row2.SubItems.Add("");
row2.SubItems.Add("B");

listView1.Items.Add(row2);


// Add a SubItem to existing item (row1)
System.Windows.Forms.ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
item1.Text = "C";

listView1.Items[0].SubItems[2] = item1;

Result

listView

1 Comment

@Rubel Hosen Has my solution worked well? Solved your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.