2012-07-06 52 views
0

下面我有一些代碼來從列表視圖中選定的行獲取索引。但是這隻有在用戶點擊第一列時纔有效。但是,如果我有四列,我該怎麼做,讓用戶點擊listView的一行上的任何地方?獲取列表中的幾個列索引

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

回答

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

+0

嗯,看起來有點複雜比較其他答案,但無論如何感謝! – 2012-07-06 07:21:59