从组合框保存选定的项目,combox由数据库值填充 [英] saving selected item from combobox , combox is filled by database values
问题描述
我在从组合框中保存所选项目时遇到问题。 ,组合框由数据库值填充。
我的代码如下。我已经制作了一个方法viewincombo(string str)。
private void viewincombo(string str )
{
string connectionString =(@Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Documents and Settings \\\
ic-15 \\ \\我的文件\ ALRS.mdb);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
// string str =select thana from thana;
OleDbDataAdapter da = new OleDbDataAdapter(str,con);
DataSet ds = new DataSet();
da.Fill(ds);
comboBoxView.Visible = true;
comboBoxView.DataSource = ds.Tables [0];
}
和我这样调用这个方法
private void buttonViewCombo_Click(object sender,EventArgs e)
{
string str =select * from Licence_Category;
viewincombo(str);
comboBoxView。 DisplayMember =Licence_Category;
}
表名为Licence_Category,字段名为licence_category。
以上代码正常工作但
textBoxField.Text = comboBoxView.SelectedItem.ToString();
不起作用。
提前谢谢。
I am having problem in saving selected item from combo box. , combo box is filled by database values.
my code is given below.I have made a method "viewincombo(string str)".
private void viewincombo(string str)
{
string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\nic-15\My Documents\ALRS.mdb");
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
//string str = "select thana from thana";
OleDbDataAdapter da = new OleDbDataAdapter(str, con);
DataSet ds = new DataSet();
da.Fill(ds);
comboBoxView.Visible = true;
comboBoxView.DataSource = ds.Tables[0];
}
and i am calling this method like this
private void buttonViewCombo_Click(object sender, EventArgs e)
{
string str = "select * from Licence_Category";
viewincombo(str);
comboBoxView.DisplayMember = "Licence_Category";
}
Table name is Licence_Category and field name is licence_category.
Above code is working but
textBoxField.Text = comboBoxView.SelectedItem.ToString();
doesnt not works.
Thanks in advance.
推荐答案
你也可以使用
textBoxField.Text = comboBoxView.Text;
而不是
textBoxField.Text = comboBoxView.SelectedItem.ToString();
这篇关于从组合框保存选定的项目,combox由数据库值填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!