gpt4 book ai didi

c# - 将所选行从 ListView 显示到文本框?

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

如何将listview选中的行显示到textBox中?

这就是我对 int dataGridView 的处理方式:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ReadOnly = true;
if (dataGridView1.SelectedRows.Count != 0)
{
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
EmpIDtextBox.Text = row.Cells["EmpID"].Value.ToString();
EmpNametextBox.Text = row.Cells["EmpName"].Value.ToString();
}
}

我试过这个:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem item = listView1.SelectedItems[0];
if (item != null)
{
EmpIDtextBox.Text = item.SubItems[0].Text;
EmpNametextBox.Text = item.SubItems[1].Text;
}
}

最佳答案

您可能想先检查是否有 SelectedItem。 When the selection changed, ListView would actually unselect the old item then select the new item, hence triggering listView1_SelectedIndexChanged twice.除此之外,您的代码应该可以工作:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
EmpIDtextBox.Text = item.SubItems[0].Text;
EmpNametextBox.Text = item.SubItems[1].Text;
}
else
{
EmpIDtextBox.Text = string.Empty;
EmpNametextBox.Text = string.Empty;
}
}

关于c# - 将所选行从 ListView 显示到文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599358/

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