Hi,

I have DataTable 's column that is a source of rows for combo box - it loads rows like it should. But, binding ComboBox.SelectedValue does nothing. I think that this is some problem with comparison between the value provided by BindingSource and the values from ComboBox . BindingSource.Position iterates through collumn correctly (I've checked)

How can I make it work? Is there any way that I can change the way it compares values (I bet there is, but HOW)??

Recommended Answers

All 5 Replies

Few days ago I've just solved that MS problem.

For beginning I'm using MS VS 2003 with an old Compact framework (note that compact framework is very short version of real framework).

So basically, it seems that Microsoft solved this problem in VS 2005 and 2008, but I didn't tested that.
So far this is my solution that works (100% in my app). There two key points of solution: first is seting new BindingContext to the combo box, and second is seting DataSource property of combo box to DataView !!!
If you miss some of those two key points, selectedValue property will not work correctly for sure.
So this is few lines of code that provides you seting selectedValue of combo box at your desired moment (C# code):

/*
Suppose we have some DataTable as data set, called dt. ComboBox name is "cmb". Combo's property DisplayMember is "dispMname", and ValueMember is "valMname". The value to assign to selectedValue is "val" of type long. So, here is the code
*/
//CODE BEGIN
cmb.BindingContext = new BindingContext();
cmb.DisplayMember = "dispMname";
cmb.ValueMember = "valMname";
cmb.DataSource = dt.DefaultView;
cmb.SelectedValue = long.Parse(val.Tostring()); //This casting is stupid, but....
//END OF CODE

Hope this will help !

Regards Peter !

sorry for my english
the problem is this:
when you set the ComboBox.Text with the value wich is not in the collection
ComboBox.SelecteValue is set to Nothing but BindinSource.Position holds
last value, when you cancel the operation you set
BindingSource.Position = "Previous Value"
but BindingSource.Position is already set to "Previous Value" then in this case
there is no change then no notification has sent to ComboBox which
hold wrong value, you have to force rebinding

try this:

BindingSource.ResetBindings(False)
hi Ant

sorry for my english
the problem is this:
when you set the ComboBox.Text with the value wich is not in the collection
ComboBox.SelecteValue is set to Nothing but BindinSource.Position holds
last value, when you cancel the operation you set
BindingSource.Position = "Previous Value"
but BindingSource.Position is already set to "Previous Value" then in this case
there is no change then no notification has sent to ComboBox which
hold wrong value, you have to force rebinding

try this:

BindingSource.ResetBindings(False)
hi Ant

sorry for my english
the problem is this:
when you set the ComboBox.Text with the value wich is not in the collection
ComboBox.SelecteValue is set to Nothing but BindinSource.Position holds
last value, when you cancel the operation you set
BindingSource.Position = "Previous Value"
but BindingSource.Position is already set to "Previous Value" then in this case
there is no change then no notification has sent to ComboBox which
hold wrong value, you have to force rebinding

try this:

BindingSource.ResetBindings(False)
hi Ant

Thanks for helping.
But did you notice that this thread is nearly 3 years old.
I am sure that the original problem is solved by now.

What if someone has same problem?

commented: It may not be exactly the same. Create a new thread. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.