0
我有一个id文本框和一个名称,部门组合框和一个分区,我已将它们与MS Access关联,但部门和分区组合框仅显示所选值不是包含所有项目的列表),我需要它给我一个包含所有项目的列表,并重点关注该ID的当前值。在Combobox中显示列表项并关注来自数据库的预留值
这是我到目前为止有:
private void id_txt_TextChanged(object sender, EventArgs e)
{
try
{
bool hasdata = false;
if (id_edit.Text != "")
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
String query = " SELECT * FROM emp_personal WHERE ID = " + Int32.Parse(id_edit.Text) + "";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection);
while (reader.Read())
{
hasdata = true;
name_edit.Text = reader["Name"].ToString();
DataSet ds = new DataSet();
adapter.Fill(ds);
department_edit.DataSource = ds.Tables[0];
department_edit.DisplayMember = "Department";
department_edit.ValueMember = "Department";
division_edit.DataSource = ds.Tables[0];
division_edit.DisplayMember = "Division";
division_edit.ValueMember = "Division";
}
connection.Close();
}
if (!hasdata)
{
name_edit.Clear();
department_edit.SelectedIndex = -1;
division_edit.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
connection.Close();
}
}
感谢。