lemoos
lemoos

Reputation: 167

Updating the value of a subItem in a ListViewItem inside a listview c# (Winforms)

I would like to update the data contained in a ListviewItem contained itself in a ListView. The idea is when a row of the listview is selected, I click on a button and the data is updated .

I have this code :

ListView listView1 = new System.Windows.Forms.ListView();   
ListViewItem lv1 = new ListViewItem("me");    
lv1.SubItems.Add("my brother");    
listView1.Items.Add(lv1);

Button myB = new System.Windows.Forms.Button();

private void myB_Click(object sender, EventArgs e)
{
    listView1.SelectedItems[0]  ....... ;
}

I know how to go any further to acces to modify the value of "my brother" to "my sister".

Thanks in advance.

Upvotes: 1

Views: 20608

Answers (1)

Steve
Steve

Reputation: 216293

Check if something is selected then access the first ListViewItem in the SelectedItems

if(listView1.SelectedItems != null)
{
   ListViewItem item = listView1.SelectedItems[0];
   item.SubItems[0].Text = "Sister";
}

this is the reference on MSDN for ListViewSubItem class

Upvotes: 4

Related Questions