我有这个来显示从数据库到列表视图的所有记录
private void populatelistview()
{
listView1.Items.Clear();
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select * from Employee", myDatabaseConnection))
{
SqlCommand.CommandType = CommandType.Text;
SqlDataReader dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
listView1.Items.Add(new ListViewItem(new string[] { dr["EmpID"].ToString(), dr["Lname"].ToString(), dr["Fname"].ToString() }));
}
}
}
}
例如我有这个结果:
EmpID | Lname | Fname
40001 | Smith | John
40002 | Jones | David
40003 | Bryan | Kobe
我将如何以编程方式从上面的列表中选择一个项目?例如,我在 textBox 中输入 40002 然后这将被选中40002 | Jones | David