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

北战南征 提交于 2019-12-12 03:47:46

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10182463/updating-the-value-of-a-subitem-in-a-listviewitem-inside-a-listview-c-sharp-win

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!