Reputation: 51
sorry to bother, but I have a problem with C# Winforms, since I've been searching for the same problem, I found some solutions but they're not working for me. OK, I have a Bindinglist with objects
BindingList<objects.usuario> usuarios = new BindingList<objects.usuario>();
The objects have some public strings and int variables, one string and one int are the variables that I need.
public string dataNombreCompleto;
public int dataIdUsuario;
So, once the list "usuarios" has some objects, I do this
cbAdministrativos.DisplayMember = "dataNombre";
cbAdministrativos.ValueMember = "dataIdUsuario";
cbAdministrativos.DataSource = usuarios;
And the thing is, it's not working, the combobox (cbAdministrativos) still displays the object.
When I go through the debugger, after a breakpoint, the combobox sets the display member, the valuemember and the datasource, as the code goes, but, in the next instruction (the end of the method), I realized that the displaymember, magically, turns to "" instead the string "dataNombre".
Any idea?
Thanks in advance, and sorry about the bad english.
SORRY AND THANKS TO ALL! In my desperation, I tried with different fields in the object, and I didn't realize that I left "dataNombre" instead of "dataNombreCompleto" for the example code in the question, anyway that's correct, that's the original DisplayMember in my original code, but, the problem's still there :(
Upvotes: 4
Views: 8862
Reputation: 409
It seems that when you set the DataSource property, the ComboBox checks if DisplayMember exist as a property (/field? don't know, never tried fields here) and if it doesn't, then the ComboBox clears the DisplayMember property.
It would be better if the ComboBox generated an exception (fail early) instead of hiding the failure.
Upvotes: 0
Reputation: 436
Had similar problem. Try to assign the DataSource first:
cbAdministrativos.DataSource = usuarios;
cbAdministrativos.DisplayMember = "dataNombre";
cbAdministrativos.ValueMember = "dataIdUsuario";
Upvotes: 0
Reputation: 5986
In my case, it was the Sorted property which was set to true
in the designer.
According to the doc, an exception is thrown when this is set to true while databinding is on but apparently no exception in thrown if you set the DataSource
and XMember
properties after the Sorted
property is set to true
; it just silently doesn't work and you pull your precious hair out until there's none left.
Upvotes: 3
Reputation: 196
You'd need to use getter and setter in you usuario class.
string dataNombreCompleto;
int dataIdUsuario;
public string _DataNumComp
{
get
{
return dataNombreCompleto;
}
set
{
dataNombreCompleto = value;
}
}
public int _ID
{
get
{
return dataIdUsuario;
}
set
{
dataIdUsuario = value;
}
}
Then modify the binding code as required. For example, look at the given snippet of code:
cbAdministrativos.ValueMember = "_ID";
cbAdministrativos.DisplayMember = "_DataNumComp";
cbAdministrativos.DataSource = usuarios;
I hope this will resolve your issue.
Upvotes: 3
Reputation: 13114
It should be:
cbAdministrativos.DisplayMember = "dataNombreCompleto";
Also, consider using Public Properties instead of Public Fields.
Change this:
public string dataNombreCompleto;
public int dataIdUsuario;
To this:
public string dataNombreCompleto { get; set; }
public int dataIdUsuario { get; set; }
Upvotes: 9
Reputation: 1576
What are the objects that you want to set as Display Member
and Value Member
? Are these two from your question?
public string dataNombreCompleto;
public int dataIdUsuario;
Then, you can set dataNombreCompleto as Display Member
and dataIdUsuario as Value Member
.
cbAdministrativos.DisplayMember = "dataNombreCompleto";
cbAdministrativos.ValueMember = "dataIdUsuario";
cbAdministrativos.DataSource = usuarios;
DisplayMember
property, it is designed to display strings in
the comboboxes. ValueMember
is hidden behind DisplayMember
.ValueMember
property, it is designed to get values that
correspond to the selections in the drop-down list.For better understanding on DisplayMember
and ValueMember
property, you might refer to:
Upvotes: 2
Reputation: 196
You need to set the exact name of the displayMember. Try this
cbAdministrativos.DisplayMember = "dataNombreCompleto";
Upvotes: 2