Hi

im a beginner and just need some help solving this problem. i think its simple but have tried to figure it out but im getting no where. i currently have four text boxes to which i add data to, i then click the add and want to add this to the listview. i am able to add the first time but then can not add again also this does not add data from the first text box to the first column but leaves the first column out and starts from the second column.

below is the code

ListViewItem item1 = new ListViewItem();
                                
                // add items.
           item1.SubItems.Add((Convert.ToString(StudentHouses.HouseNo)));
           item1.SubItems.Add((StudentHouses.AddressLine1));
           item1.SubItems.Add((StudentHouses.AddressLine2));
           item1.SubItems.Add((StudentHouses.AddressLine3));
            
            //Add the items to the ListView.
            listView1.Items.Add( item1 );
 // Add the ListView to the control collection.
            this.Controls.Add(listView1);

Any help would be appreciated.
thankyou

Recommended Answers

All 6 Replies

Wrong forum :)

But it skips the first column because you haven't added a name to the ListViewItem, just added SubItems. item1.Text = "something" should go in the first column.

The listview should already be part of the controls collection if you added it to the screen in the designer. Adding it again and again is going to cause issues, which I believe you are seeing. Leave out the last two lines.

Also put your code in [code] [/code] tags. It makes it format better and allows us to refer to specific lines in the code (it numbers the lines).

ive got it working to add to the first column, the reason for this was the first time it should have been:

item1.Text = Convert.ToString(StudentHouses.HouseNo);

and their after you use the subitem method.
however i still can not figure out how to add multiple times to the listview

ive got it working to add to the first column, the reason for this was the first time it should have been:

Which is what I said :)

however i still can not figure out how to add multiple times to the listview

Create another ListViewItem and add it:

for (int i = 0; i < 10; i++) {
    ListViewItem lvi = new ListViewItem();
    lvi.Text = i.ToString();
    lvi.SubItems.Add("2nd power = " + (i * i));
    lvi.SubItems.Add("3rd power = " + (i * i * i));
    lvi.SubItems.Add("4th power = " + (i * i * i * i));
    myListView.Add(lvi);
}

This adds 10 items to the list view, each with 3 sub items.

// make sure colomn headers are set to number of strings in listview
// the first string will be the item while the second third and fourth are the sub items.
// use the for statement will add new listview item with 3 more subitems

ListViewItem lvi = new ListViewItem(new string[] {"first string","second string", "third string", "fourth string"});
listView1.Items.Add(lvi);

put all the text box contents into an array of string contents
item.Text=contents[0];
item.SubItems.Add(contents[1]);
item.SubItems.Add(contents[2]);
item.SubItems.Add(contents[3]);
listView1.Items.Add(item);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.