gpt4 book ai didi

c# - 从 ListView 控件中获取值(value)

转载 作者:太空狗 更新时间:2023-10-30 00:05:17 25 4
gpt4 key购买 nike

需要帮助从 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;
}

最佳答案

ListView 没有属性 SelectedIndex。您应该使用 SelectedItemsSelectedIndices

所以你可以使用这个:

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# - 从 ListView 控件中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14708244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com