需要帮助从ListView中的custID列中选择值,以便我可以从数据库中检索值并将其显示在TextBox中.SelectedIndex不在c#中工作
由于
http://img713.imageshack.us/img713/133/listview.jpg
我的代码
private void yourListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (yourListView.SelectedIndex == -1)
return;
//get selected row
ListViewItem item = yourListView.Items[yourListView.SelectedIndex];
//fill the text boxes
textBoxID.Text = item.Text;
textBoxName.Text = item.SubItems[0].Text;
textBoxPhone.Text = item.SubItems[1].Text;
textBoxLevel.Text = item.SubItems[2].Text;
}
答案 0 :(得分:12)
ListView没有属性SelectedIndex
。您应该使用SelectedItems
或SelectedIndices
。
所以你可以使用它:
private void yourListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (yourListView.SelectedItems.Count == 0)
return;
ListViewItem item = yourListView.SelectedItems[0];
//fill the text boxes
textBoxID.Text = item.Text;
textBoxName.Text = item.SubItems[0].Text;
textBoxPhone.Text = item.SubItems[1].Text;
textBoxLevel.Text = item.SubItems[2].Text;
}
我在此建议将属性MultiSelect
设置为false
。
答案 1 :(得分:2)
C#和WPF使用它:
private void lv_yourListView_SelectedIndexChanged(object sender, EventArgs
e)
{
if (yourListView.SelectedItems.Count == 0)
return;
var item = lvb_listInvoices.SelectedItems[0];
var myColumnData = item.someField; //use whatever you want
}