显示Combobox中的列表项目并关注来自DB的预设值(Display List items in Combobox and Focus on presaved value from DB)

编程入门 行业动态 更新时间:2024-10-27 22:20:38
显示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(); }

更多推荐

本文发布于:2023-08-05 04:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   预设值   列表   DB   Combobox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!