C# - 用数据表填充组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/256832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Fill a combo box with a DataTable
提问by MrG
I'm used to work with Java where large amounts of examples are available. For various reasons I had to switch to C# and trying to do the following in SharpDevelop:
我习惯于使用 Java,其中有大量示例可用。由于各种原因,我不得不切换到 C# 并尝试在 SharpDevelop 中执行以下操作:
// Form has a menu containing a combobox added via SharpDevelop's GUI
// --- Variables
languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();
// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add( lName );
for( int i=0; i<languages.Length; i++ ) {
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);
// --- Handling the combobox
mnuActionLanguage.ComboBox.DataSource = myDataSet.Tables["Lang"].DefaultView;
mnuActionLanguage.ComboBox.DisplayMember = "Language";
One would assume to see some values in the dropdown, but it's empty. Please tell me what I'm doing wrong ;(
人们会假设在下拉列表中看到一些值,但它是空的。请告诉我我做错了什么;(
EDIT: mnuActionLanguage.ComboBox.DataBind() is what I also found on the net, but it doesn't work in my case.
编辑: mnuActionLanguage.ComboBox.DataBind() 也是我在网上找到的,但在我的情况下不起作用。
SOLUTION
解决方案
mnuActionLanguage.ComboBox.BindingContext = this.BindingContext;
at the end solved the problem!
最后解决了问题!
采纳答案by BlackWasp
You need to set the binding context of the ToolStripComboBox.ComboBox.
您需要设置 ToolStripComboBox.ComboBox 的绑定上下文。
Here is a slightly modified version of the code that I have just recreated using Visual Studio. The menu item combo box is called toolStripComboBox1 in my case. Note the last line of code to set the binding context.
这是我刚刚使用 Visual Studio 重新创建的代码的稍微修改版本。在我的例子中,菜单项组合框称为 toolStripComboBox1。请注意用于设置绑定上下文的最后一行代码。
I noticed that if the combo is in the visible are of the toolstrip, the binding works without this but not when it is in a drop-down. Do you get the same problem?
我注意到,如果组合在工具条的可见区域中,则绑定在没有这个的情况下有效,但在下拉列表中时无效。你有同样的问题吗?
If you can't get this working, drop me a line via my contact page and I will send you the project. You won't be able to load it using SharpDevelop but will with C# Express.
如果您无法完成此工作,请通过我的联系页面给我留言,我会将项目发送给您。您将无法使用 SharpDevelop 加载它,但可以使用 C# Express。
var languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();
// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);
toolStripComboBox1.ComboBox.DataSource = myDataSet.Tables["Lang"].DefaultView;
toolStripComboBox1.ComboBox.DisplayMember = "Language";
toolStripComboBox1.ComboBox.BindingContext = this.BindingContext;
回答by Alan
This line
这条线
mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";
is wrong. Change it to
是错的。将其更改为
mnuActionLanguage.ComboBox.DisplayMember = "Language";
and it will work (even without DataBind()).
它会起作用(即使没有 DataBind())。
回答by Timothy Khouri
A few points:
几点:
1) "DataBind()" is only for web apps (not windows apps).
1) “DataBind()”仅适用于网络应用程序(不是 Windows 应用程序)。
2) Your code looks very 'JAVAish' (not a bad thing, just an observation).
2)你的代码看起来很“JAVAish”(不是坏事,只是一个观察)。
Try this:
尝试这个:
mnuActionLanguage.ComboBox.DataSource = languages;
If that doesn't work... then I'm assuming that your datasource is being stepped on somewhere else in the code.
如果这不起作用......那么我假设您的数据源正在代码中的其他地方被踩到。
回答by Ady
Are you applying a RowFilter to your DefaultView later in the code? This could change the results returned.
您是否在代码中稍后将 RowFilter 应用于您的 DefaultView?这可能会改变返回的结果。
I would also avoid using the string as the display member if you have a direct reference the the data column I would use the object properties:
如果您直接引用数据列,我也会避免使用字符串作为显示成员,我将使用对象属性:
mnuActionLanguage.ComboBox.DataSource = lTable.DefaultView;
mnuActionLanguage.ComboBox.DisplayMember = lName.ColumnName;
I have tried this with a blank form and standard combo, and seems to work for me.
我已经用空白表格和标准组合尝试过这个,似乎对我有用。
回答by Nidz
string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True";
SqlConnection Con = new SqlConnection(strConn);
Con.Open();
string strCmd = "select companyName from companyinfo where CompanyName='" + cmbCompName.SelectedValue + "';";
SqlDataAdapter da = new SqlDataAdapter(strCmd, Con);
DataSet ds = new DataSet();
Con.Close();
da.Fill(ds);
cmbCompName.DataSource = ds;
cmbCompName.DisplayMember = "CompanyName";
cmbCompName.ValueMember = "CompanyName";
//cmbCompName.DataBind();
cmbCompName.Enabled = true;
回答by Masoud Siahkali
For example, i created a table :
例如,我创建了一个表:
DataTable dt = new DataTable ();
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Value", typeof(int));
Add recorde to table :
将记录添加到表:
DataRow row = dt.NewRow();
row["Title"] = "Price"
row["Value"] = 2000;
dt.Rows.Add(row);
or :
或者 :
dt.Rows.Add("Price",2000);
finally :
最后 :
combo.DataSource = dt;
combo.DisplayMember = "Title";
combo.ValueMember = "Value";