我对C#还是很陌生,由于我发现其中的简单性,因此我正在使用Access数据库。在列表框中选择该项目时,我需要在文本框中显示该项目的值
当前我有这段代码,它确实在文本框中显示了值,但是如果我从列表框中选择一个项目,它总是弹出的同一项目
private void EditItemBrowserBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from ItemsList";
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
EditIDTB.Text = reader["ID"].ToString();
ItemNameAddTB.Text = reader["ItemName"].ToString();
ItemTypeAddTB.Text = reader["ItemType"].ToString();
ItemBrandAddTB.Text = reader["ItemBrand"].ToString();
ItemPriceAddTB.Text = reader["ItemPrice"].ToString();
}
以及form_load()的代码
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from ItemsList";
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
AvItemsBox.Items.Add(reader["ItemBrand"].ToString() + " " + reader["ItemName"].ToString() + " " + reader["ItemType"].ToString() + " " + reader["ItemPrice"].ToString());
EditItemBrowserBox.Items.Add(reader["ItemBrand"].ToString() + " " + reader["ItemName"].ToString() + " " + reader["ItemType"].ToString() + " " + reader["ItemPrice"].ToString());
}
connection.Close();
我希望它显示我在每个文本框中选择的项目的相应值,而当我单击列表框中的项目时,它仅显示随机项目