2015-04-04 76 views
-2

所选项目如何获得行号从列表视图中选择的项目在C#获取行号从列表视图C#

private void listView1_DoubleClick(object sender, EventArgs e) 
{ 
    if((listView1 row numer for selectitem) > 2) 
    { 
     int indx = listView1.SelectedItems[0].Index; 
     listView1.Items[indx].Remove(); 
    } 
} 
+0

你的意思是问如何获得索引? https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindices(v=vs.110).aspx – 2015-04-04 10:37:38

+0

如果我选择项目我需要得到行号为这个选择 – 2015-04-04 10:57:05

回答

0

尝试SelectedIndexChanged事件,而不是...指数位置是从零开始的,所以如果你有10个项目的指标将是0 - 9。如果你想0是那么行1只需添加一个。最后,当没有选择的项指数为-1

private void ListView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (ListView1.SelectedIndex > -1) 
    { 
     // Add 1 so you have 1 - 10 instead of 0 - 9 
     int rowNumber = ListView1.SelectedIndex + 1; 

     // Your example says you want to delete the selected index 
     // so you still would want to use the selected index 
     ListView1.Items.RemoveAt(ListView1.SelectedIndex); 

     // After you remove the item, this method will fire again 
     // but the selected index will be -1 so none of this code will 
     // execute again. 
    } 
} 

请记住,此代码只支持一次选择一行。

希望这有助于...

0

https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.itemselectionchanged(v=vs.110).aspx可能会有所帮助。

也就是说,请监听ItemSelectionChanged事件,并在事件数据上使用ItemIndex属性。

E.g.微软的例子显示

private void ListView1_ItemSelectionChanged(
    Object sender, ListViewItemSelectionChangedEventArgs e) { 

    var i = e.ItemIndex; // got the latest selection 
} 
+0

谢谢但如果您在列表视图中选择任何项目,需要该项目的门禁行号,请选择 – 2015-04-04 10:54:34

+0

尝试更新答案以反映我认为您所要求的内容。我很抱歉地说语言障碍很高 - 我不完全了解你的英语,并且在猜测你的意图。 – 2015-04-04 14:28:55