C# - 用数据表填充组合框
我习惯使用 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";
人们会假设在下拉列表中看到一些值,但它是空的。 请告诉我我做错了什么;(
编辑: mnuActionLanguage.ComboBox.DataBind() 也是我在网上找到的,但它在我的情况下不起作用。
解决方案
mnuActionLanguage.ComboBox.BindingContext = this.BindingContext;
最后解决了问题!
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:
// 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.
SOLUTION
mnuActionLanguage.ComboBox.BindingContext = this.BindingContext;
at the end solved the problem!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要设置 ToolStripComboBox.ComboBox 的绑定上下文。
这是我刚刚使用 Visual Studio 重新创建的代码的稍微修改版本。 在我的例子中,菜单项组合框称为 toolStripComboBox1。 请注意设置绑定上下文的最后一行代码。
我注意到,如果组合位于工具条的可见区域中,则绑定可以在没有此操作的情况下工作,但当它位于下拉列表中时则不行。 你也遇到同样的问题吗?
如果您无法正常工作,请通过我的联系页面给我留言,我会将项目发送给您。 您将无法使用 SharpDevelop 加载它,但可以使用 C# Express 加载它。
You need to set the binding context of the 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.
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.
例如,我创建了一个表:
将记录添加到表:
或:
最后:
For example, i created a table :
Add recorde to table :
or :
finally :
您稍后会在代码中将 RowFilter 应用于 DefaultView 吗? 这可能会改变返回的结果。
如果您直接引用数据列,我也会避免使用字符串作为显示成员,我将使用对象属性:
我已经使用空白表单和标准组合尝试过此操作,并且似乎对我有用。
Are you applying a RowFilter to your DefaultView later in the code? This could change the results returned.
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:
I have tried this with a blank form and standard combo, and seems to work for me.
几点:
1)“DataBind()”仅适用于Web应用程序(不适用于Windows应用程序)。
2)你的代码看起来非常“JAVAish”(不是坏事,只是一个观察)。
试试这个:
如果这不起作用......那么我假设您的数据源正在代码中的其他地方被踩到。
A few points:
1) "DataBind()" is only for web apps (not windows apps).
2) Your code looks very 'JAVAish' (not a bad thing, just an observation).
Try this:
If that doesn't work... then I'm assuming that your datasource is being stepped on somewhere else in the code.
这条线
是错误的。 将其更改为
并且它将起作用(即使没有 DataBind())。
This line
is wrong. Change it to
and it will work (even without DataBind()).