Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

ListView add new row with Textbox text?

I've found many articles about adding an "Item" which is in many cased incorrectly refered to as a 'Row'.

I need to add a 'New Row" to a listview with data from two textboxes.

So far the code I have inserts the data into the listview, but always overwrites the text in the listview top rows.

A row consists of the first left-hand column ('Item')  and another column next to it ('SubItem').

[Code]
 private void btnAddNew_Click(object sender, EventArgs e)
        {            
            listView1.Items.Add(textBox1.Text);
            listView1.Items[0].SubItems.Add(textBox2.Text);
}
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr
kvnsdr

ASKER

So. textBox1 would code like so?

listView1.Items[listView1.Items.Count - 1].Add(textBox1.Text);
No. You are adding a new one to the end of the list, so that one's OK.
 private void btnAddNew_Click(object sender, EventArgs e)
        {            
            listView1.Items.Add(textBox1.Text);
            listView1.Items[listView1.Items.Count - 1].SubItems.Add(textBox2.Text);
       }