我有一個列表框。每當我從數據庫中搜索某個人時,結果都將顯示在列表框中。然後我想要的是每當我從列表框中點擊人物的名字時,人物細節就會顯示在文本框中。我有我的代碼,但問題是隻有我第一次點擊的人的詳細信息顯示在文本框中。在文本框中顯示詳細信息
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
connection.Open();
OleDbCommand select = new OleDbCommand();
select.Connection = connection;
select.CommandText = "Select * From Accounts";
OleDbDataReader reader = select.ExecuteReader();
while (reader.Read())
{
if (reader[0].ToString() == listBox1.Tag.ToString())
{
fnametb.Text = reader[1].ToString();
lnametb.Text = reader[2].ToString();
agetb.Text = reader[3].ToString();
addresstb.Text = reader[4].ToString();
coursetb.Text = reader[5].ToString();
}
}
connection.Close();
}
你引用'listBox1.Tag.ToString()',而不是選擇列表框項目。 – 2013-03-01 07:16:14
是'listBox1.Tag.ToString()'和'listBox1.selectedItem.ToString()'是一樣的嗎? – Pyromancer 2013-03-01 07:17:03
將其更改爲'listBox1.selectedItem.ToString()' – Pyromancer 2013-03-01 07:17:20