winforms 从listview到textBox显示所选行吗?

kmbjn2e3  于 2023-05-18  发布在  其他
关注(0)|答案(3)|浏览(140)

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

vqlkdk9b1#

您可能需要先检查是否存在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;
    }
}
8dtrkrch

8dtrkrch2#

//在c#中选择行列表视图检查
foreach(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();


            }

        }
c2e8gylq

c2e8gylq3#

只需选择行。迭代列表并检查选中了哪一行。按所选行进行操作。比如

private void delete_Items(object sender, EventArgs e)
{

    foreach(ListViewItem item in listView1.Items)
    {

      if (item.Selected == true)
      {
          // Code Here...
      }

    }

}

相关问题