简体   繁体   English

C#-用数据表填充组合框

[英]C# - Fill a combo box with a DataTable

I'm used to work with Java where large amounts of examples are available. 我曾经使用Java,那里有大量的示例。 For various reasons I had to switch to C# and trying to do the following in SharpDevelop: 由于各种原因,我不得不切换到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! 最终解决了问题!

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. 这是我刚刚使用Visual Studio重新创建的代码的略微修改版本。 The menu item combo box is called toolStripComboBox1 in my case. 在我的案例中,菜单项组合框称为toolStripComboBox1。 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. 您将无法使用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;
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;

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";

Are you applying a RowFilter to your DefaultView later in the code? 您是否在稍后的代码中将RowFilter应用于DefaultView? 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: 如果您直接引用数据列,我也将避免使用字符串作为显示成员,我将使用对象属性:

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. 我已经使用空白表格和标准组合尝试了此操作,似乎对我有用。

A few points: 几点:

1) "DataBind()" is only for web apps (not windows apps). 1)“ DataBind()”仅适用于Web应用程序(不适用于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. 如果这不起作用...那么我假设您的数据源正在踩到代码中的其他位置。

This line 这条线

mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";

is wrong. 是错的。 Change it to 更改为

mnuActionLanguage.ComboBox.DisplayMember = "Language";

and it will work (even without DataBind()). 并且它将起作用(即使没有DataBind())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM