C# 将列表视图中的选定行显示到文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17599358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display the selected row from listview to textBox?
提问by Karlx Swanovski
How to display the selected row from listview to textBox?
如何将列表视图中的选定行显示到文本框?
This is how I do int dataGridView:
这就是我如何做 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();
}
}
I tried this:
我试过这个:
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;
}
}
采纳答案by Fung
You may want to check if there is a SelectedItem first. When the selection changed, ListViewwould actually unselect the old item then select the new item, hence triggering listView1_SelectedIndexChangedtwice. Other than that, your code should work:
您可能想先检查是否有 SelectedItem。When the selection changed, ListViewwould actually unselect the old item then select the new item, hence triggering listView1_SelectedIndexChangedtwice. 除此之外,您的代码应该可以工作:
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;
}
}
回答by TaTa
// select row listview check in c#
// 在 C# 中选择行列表视图检查
foreach (ListViewItem itemRow in taskShowListView.Items) {
foreach(taskShowListView.Items 中的 ListViewItem itemRow){
if (itemRow.Items[0].Checked == true)
{
int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);
string taskDate = itemRow.SubItems[1].ToString();
string taskDescription = itemRow.SubItems[2].ToString();
}
}

