Can (Combobox) given from Country datatable?

VugarM

New member
Joined
Jun 30, 2007
Messages
3
Programming Experience
3-5
I have like this,
There are form with binded combobox and textbox. Before editing the data in form the combobox shown normally (when currencymanager position changed).
Form data : ID Name Country
1, x, (3)Can (Combobox) given from Country datatable
2, y, (2)US
3, z, (1)Az
Country data: ID Country
1, Az
2, US
3, Can
But as soon I was changed Country f.e. for y name to 1(Az), after updating the Customer Datatable for all customers where Country is Az (First value in combobox) shown "System.Data.DataRowView" (Az also include in combobox list but not highligted). If I sort Country datatable as desc same will appear with US.
 
Here is the test project, try it...

I think it is a Microsoft bug, use attachment for testing. (Before start just change the path for database, it under comment)
After start project:
1) Load data
2) Change Country for any Customer to Russia (first item in combobox list)
3) update
4) change position to any Customer from Russia

You should see System.Data.DataRowView instead of Russia.
 

Attachments

  • Bug.zip
    25.6 KB · Views: 16
Last edited by a moderator:
Its not a bug. . you have bound too many things!

You bound .SelectedItem to dataset1.Country.Country
AND you bound .SelectedValue to dataset1.Country.CountryID

Remove the binding from .SelectedItem, leaving just the binding on .SelectedValue
Now, whatever is the content of .ValueMember will be written into whatever table is named in .SelectedValue

ie your combo:
.DataSource = Country List
.DisplayMember = Country name, like "Russia"
.ValueMember = Country ID like "1"

.SelectedValue = binding to Person.CountryID


Problem was, your .SelectedValue was "1" for russia, but .SelectedItem really was an entire DataRowView - an object that holds a datarow. By binding .SelectedItem you were causing a DataRowView object to be forced into your Person data table, rather than a String or Number of "1"

Hope you understand this, but if you dont, heres the simple rule:
NEVER, EVER BIND .SelectedItem - in 99.9% of cases is is NOT what you want
 

Attachments

  • Image1.png
    Image1.png
    11.4 KB · Views: 26
Thanks Cjard it helped, if it is not bored you could you explain
1) Why need then Selecteditem?
2) As I understood the selecteditem get datarowview obect instead of datarowview.row.item, why it happen only when selectedindex=0?

Anyway big thanks, everytime when I find my own mistake a'm glad cause it can be replaced. My Respect.
 
Thanks Cjard it helped, if it is not bored you could you explain
1) Why need then Selecteditem?
You dont, and you certainly dont need to bind it in this case

I'm not really wanting to launch into a big explanation on this, because youre basically uising something incorrectly and I'm saying dont do it because it isnt for what youre using it for, and then you asking me what it IS for

Thats like you asking me why String.Append wont print to a laser printer - because it's not intended for that - and then you asking me "so what is String.Append for?"

We could go on like that for everything in the framework. If you wanted to know what .SelectedItem is for, then google it. Remember that any property of any object can be data bound. Just because it appears in the "Databindings" area of the proeprty grid in the designer, doesnt mean you have to use it. Just because you think the name looks like something that you might have to use, doesnt mean you have to use it.


2) As I understood the selecteditem get datarowview obect instead of datarowview.row.item, why it happen only when selectedindex=0?
You cant set selected index = 0 on a bound combo; it throws me an error. It doesnt additionally prove anything related to what you think is a "bug" - youre using binding incorrectly, combo doesnt know how to behave, you cant call it a bug when you didnt set it up correctly.

Again, I dont really want to launch into a big investigation and explanation of why you only see this with the first list item, but I would suspect that it is your setting index = 0 which is illegal, it moves to index = 1 instead, and then tries to force an object of type datarowview into a datatable column that doesnt support it.

End of the day, investigating strange behaviour on a wrong setup is not worth doing. Set the combo up correctly and it wont error, you wont think its a bug and it wont be an example of something that isnt broken that doesnt need fixing


Anyway big thanks, everytime when I find my own mistake a'm glad cause it can be replaced. My Respect.
Youre welcome :)
 
Back
Top