正在从列表视图控件获取值
本文关键字:控件 获取 视图 列表 | 更新日期: 2023-09-27 17:57:50
需要帮助从ListView中的custID列中选择值,这样我就可以从数据库中检索值并将其显示在TextBoxes中。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;
}
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
。
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
}