下面我有一些代码来从listView中的选定行获取索引。但这仅在用户点击第一列时才有效。但是,如果我有四列,我该如何让用户点击listView的一行?
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.FocusedItem.Index;
// Code here to pass the index to method....
}
答案 0 :(得分:2)
您必须将ListView.FullRowSelect
设置为true。
答案 1 :(得分:0)
你可以使用ListView.SelectedItems
属性来完成这项工作,
像这样的东西
private void ListView1_SelectedIndexChanged_UsingItems(
object sender, System.EventArgs e)
{
ListView.SelectedListViewItemCollection breakfast =
this.ListView1.SelectedItems;
double price = 0.0;
foreach ( ListViewItem item in breakfast )
{
price += Double.Parse(item.SubItems[1].Text);
}
// Output the price to TextBox1.
TextBox1.Text = price.ToString();
}
了解更多信息Go here