views:

25387

answers:

8

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!

A: 

This line

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

is wrong. Change it to

mnuActionLanguage.ComboBox.DisplayMember = "Language";

and it will work (even without DataBind()).

Alan
Tkx Alan, I tried that too and corrected it above - but the behaviour unfortunately didn't change :( Tkx anyway!
MrG
Well, I copy/pasted your code into VS 2008, fixed it, ran it, and I had 'English' and 'German' in the combobox. Isn't this what you needed?
Alan
Oh, now I see, you also ran into the BindingContext issue and fixed it yourself. Fine then :)
Alan
A: 

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.

Timothy Khouri
mnuActionLanguage.ComboBox.DataSource = languages;doesn't help either, the combobox remains empty. Tkx anyway!
MrG
A: 

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.

Ady
No, there is no RowFilter anywhere in the code. It seems that the issue was entirely related to the BindingContext, for whatever reason.
MrG
+7  A: 

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.

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;
BlackWasp
Nice spot, didn't realise the combo was in a toolstrip.
Ady
Me either first time around ;-)
BlackWasp
A: 

Thanks a lot!!! by referring ur code I got much knowledge>.........

A: 

Is there any way to combine two datacolumn into one display member?

A: 

hgjkjhdbv b ssjj sjh hjlnsnkljnklfn nlsnl kn

Guru
A: 

Hi, I am using combobox in my window application but i am facing one problem i.e. i am populating data into combobox from database but i want to set selectedIndex at design time or run time with coding not from database.... please help me....

Guru