1. 程式人生 > >C# listView點選某一行獲取這一行的值

C# listView點選某一行獲取這一行的值

        //可以利用Click事件,先判斷是否選中,然後再取值
        private void listView2_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                try
                {
                    string x = listView2.SelectedItems[0].SubItems[0].Text.ToString();//選中行的第一列的值
                    string y = listView2.SelectedItems[0].SubItems[1].Text.ToString();//選中行的第二列的值
                    string z = listView2.SelectedItems[0].SubItems[2].Text.ToString();//選中行的第三列的值
                                      ......
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            else
            {
                MessageBox.Show("你選擇了"+listView2.SelectedItems.Count.ToString()+"行!");
            }
        }