Reputation: 2989
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;
}
}
Upvotes: 5
Views: 43387
Reputation: 227
Simply select the row. Iterate over the list and check which row is selected. Make operation as per the row selected. Such as,
private void delete_Items(object sender, EventArgs e)
{
foreach(ListViewItem item in listView1.Items)
{
if (item.Selected == true)
{
// Code Here...
}
}
}
Upvotes: 0
Reputation: 19
// 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();
}
}
Upvotes: 0
Reputation: 3558
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;
}
}
Upvotes: 7