C# - Fill a combo box with a DataTable
Posted
by MrG
on Stack Overflow
See other posts from Stack Overflow
or by MrG
Published on 2008-11-02T12:17:51Z
Indexed on
2010/05/29
12:52 UTC
Read the original article
Hit count: 902
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!
© Stack Overflow or respective owner