将列表视图中的选定行显示为textBox?

时间:2013-07-11 17:03:51

标签: c# winforms listview textbox

如何将列表视图中的选定行显示到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;
    }
}

2 个答案:

答案 0 :(得分:6)

您可能想先检查是否有SelectedItem。选择更改后,ListView实际上会取消选择旧项目,然后选择新项目,从而触发listView1_SelectedIndexChanged两次。除此之外,您的代码应该有效:

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;
    }
}

答案 1 :(得分:0)

//选择C#中的行列表视图签入

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();



            }


        }