根据来自combox的选定项目,从数据库显示texbox中的文本 [英] Displaying text in texbox from databse based on selected items from combox
问题描述
我基本上有一个组合框和一个文本框。我该怎么办是组合框由主题代码组成,而文本框由主题名称组成,存储在数据库中。我是否知道有什么方法可以选择组合框中的项目时,根据选择的主题代码自动显示主题名称,该主题代码将来自数据库。 belwo是我的代码,但没有工作
私人 void FormAttendanceFaculty_Load( object sender,EventArgs e)
con.Open() ;
OleDbDataAdapter oda1 = new OleDbDataAdapter( select来自主题的代码,其中代码如'%',con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxCode.DataSource = dt1;
comboBoxCode.DisplayMember = code;
comboBoxCode.SelectedIndex = -1;
string myquery = SELECT name FROM subject其中name =' + comboBoxName.Text + ';
使用( var command = new OleDbCommand(myquery,con))
{
MyTextBox.Text = command.ExecuteScalar()。ToString();
}
}
我的尝试:
我试过基于通过在线和msdn编写的代码。但仍然没有工作
可能最简单的方法是同时使用DisplayMember
和ValueMember
ComboBox的属性例如
private void FormAttendanceFaculty_Load( object sender,EventArgs e)
{
con.Open();
OleDbDataAdapter oda1 = new OleDbDataAdapter( select代码,来自主题的名称,其中代码不为空,con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxCode.DisplayMember = code;
comboBoxCode.ValueMember = name;
comboBoxCode.DataSource = dt1;
comboBoxCode.SelectedIndex = -1;
}
请注意,我正在从主题表中检索代码和名称,并且我已将代码更改为' %'检查非空。我在设置显示和价值成员之后设置
DataSource
。
要在ComboBox更改时更改TextBox的内容,可以使用SelectedIndexChanged
事件,但请注意它将是当你用数据加载ComboBox时调用,所以你需要处理它,例如private void comboBoxCode_SelectedIndexChanged(对象发件人,EventArgs e)
{
如果(comboBoxCode.SelectedValue!= null )
MyTextBox.Text = comboBoxCode.SelectedValue.ToString();
else
MyTextBox.Text = 跨度>;
}
更好的选择是使用SelectionChangeCommitted
事件(reference [ ^ ])as仅当用户选择ComboBox中的值时才会引发,例如private void comboBoxCode_SelectionChangeCommitted( object sender,EventArgs e)
{
MyTextBox.Text = comboBoxCode.SelectedValue.ToString( );这个事件中的
}
comboBoxName_SelectedIndexChangedlike this
< pre lang = C#> private void comboBoxName_SelectedIndexChanged( object sender,EventArgs e)
{
string myquery =& quot; SELECT name FROM subject 其中 name =&#39;& quot; + comboBoxName.Text +& quot;&#39;& quot ;;
} < / pre > 跨度>
I have basically a combobox and and a text box. What should i do is the combobox consist of subject code whereas the textbox consist of subject name which is stored in database. Can i know is there any ways that when an item from combobox is chosen, it is automatically display subject name based on subject code chosen which will be from the database. belwo is my code but not working
private void FormAttendanceFaculty_Load(object sender, EventArgs e)
con.Open();
OleDbDataAdapter oda1 = new OleDbDataAdapter("select code from subject where code like '%'", con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxCode.DataSource = dt1;
comboBoxCode.DisplayMember = "code";
comboBoxCode.SelectedIndex = -1;
string myquery = "SELECT name FROM subject where name = '" +comboBoxName.Text + "'";
using (var command = new OleDbCommand(myquery, con))
{
MyTextBox.Text = command.ExecuteScalar().ToString();
}
}
What I have tried:
I tried based on the code whic ilearn via online and msdn written. But still not working
Probably the simplest way is to utilise both theDisplayMember
and theValueMember
properties of the ComboBox E.g.
private void FormAttendanceFaculty_Load(object sender, EventArgs e) { con.Open(); OleDbDataAdapter oda1 = new OleDbDataAdapter("select code, name from subject where code is not null", con); DataTable dt1 = new DataTable(); oda1.Fill(dt1); comboBoxCode.DisplayMember = "code"; comboBoxCode.ValueMember = "name"; comboBoxCode.DataSource = dt1; comboBoxCode.SelectedIndex = -1; }
Note that I'm retrieving both code AND name from the subject table and I've changed yourcode like '%'to check for not null. Also I'm setting the
DataSource
after I've set up the Display and Value members.
To change the contents of the TextBox when the ComboBox changes you could use theSelectedIndexChanged
event but note that it is going to be called when you are loading up the ComboBox with data, so you need to handle that e.g.private void comboBoxCode_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxCode.SelectedValue != null) MyTextBox.Text = comboBoxCode.SelectedValue.ToString(); else MyTextBox.Text = ""; }
A better option is to use theSelectionChangeCommitted
event (reference[^]) as that is only raised when the User selects the value in the ComboBox e.g.private void comboBoxCode_SelectionChangeCommitted(object sender, EventArgs e) { MyTextBox.Text = comboBoxCode.SelectedValue.ToString(); }
in this eventcomboBoxName_SelectedIndexChangedlike this <pre lang="C#">private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e) { string myquery = "SELECT name FROM subject where name = '" + comboBoxName.Text + "'"; }</pre>
这篇关于根据来自combox的选定项目,从数据库显示texbox中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!