显示Combobox中的列表项目并关注来自DB的预设值(Display List items in Combobox and Focus on presaved value from DB)
我有一个id文本框和一个名称,部门组合框和一个部门,我已经将它们与MS Access链接,但部门和部门组合框只显示所选值(不是包含所有项目的列表),我需要它给我包含所有项目的列表以及关于该ID的当前值的焦点。
这是我到目前为止:
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(); } }谢谢。
i have an id textbox and a name, department combobox and a Division, i already linked them with MS Access, but the department and Division Comboboxes only show the selected value (not a list with all the items), i need it to give me a list with all the items and a Focus on the current Value for that ID.
This is what i have so far :
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(); } }thanks.
最满意答案
一个办法 :
while (reader.Read()) { hasdata = true; name_edit.Text = reader["Name"].ToString(); department_edit.Text = String.Empty; department_edit.SelectedText = reader["Department"].ToString(); division_edit.Text = String.Empty; division_edit.SelectedText = reader["Division"].ToString(); }A solution :
while (reader.Read()) { hasdata = true; name_edit.Text = reader["Name"].ToString(); department_edit.Text = String.Empty; department_edit.SelectedText = reader["Department"].ToString(); division_edit.Text = String.Empty; division_edit.SelectedText = reader["Division"].ToString(); }更多推荐
发布评论