从listView中的多个列获取索引

时间:2012-07-06 07:05:20

标签: c#

下面我有一些代码来从listView中的选定行获取索引。但这仅在用户点击第一列时才有效。但是,如果我有四列,我该如何让用户点击listView的一行?

private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = lvRegAnimals.FocusedItem.Index;
    // Code here to pass the index to method....
}

2 个答案:

答案 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