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!
This question is related to
c#
.net
combobox
datatable
sharpdevelop
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";
This line
mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";
is wrong. Change it to
mnuActionLanguage.ComboBox.DisplayMember = "Language";
and it will work (even without DataBind()).
This line
mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";
is wrong. Change it to
mnuActionLanguage.ComboBox.DisplayMember = "Language";
and it will work (even without DataBind()).
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:
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.
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;
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:
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.
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:
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).
2) Your code looks very 'JAVAish' (not a bad thing, just an observation).
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.
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:
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.
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:
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.
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";
This line
mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";
is wrong. Change it to
mnuActionLanguage.ComboBox.DisplayMember = "Language";
and it will work (even without DataBind()).
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;
This line
mnuActionLanguage.ComboBox.DisplayMember = "Lang.Language";
is wrong. Change it to
mnuActionLanguage.ComboBox.DisplayMember = "Language";
and it will work (even without DataBind()).
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:
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.
Source: Stackoverflow.com