You can check if there is a SelectedItem first. When the selection has changed, the ListView actually deselects the old item, and then selects the new item, so listView1_SelectedIndexChanged run 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; } }
source share