简体   繁体   中英

Display the selected row from listview to textBox?

How to display the selected row from listview to textBox?

This is how I do 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;
    }
}

You may want to check if there is a SelectedItem first. When the selection changed, ListView would actually unselect the old item then select the new item, hence triggering listView1_SelectedIndexChanged twice. Other than that, your code should work:

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

// select row listview check in c#

foreach (ListViewItem itemRow in taskShowListView.Items) {

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



            }


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM