3
votes

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 :(

6
where is dataNombre property ? - Selman Genç
shouldn't the DisplayMember be dataNombreCompleto - OJay

6 Answers

9
votes

It should be:

cbAdministrativos.DisplayMember = "dataNombreCompleto";

Also, consider using Public Properties instead of Public Fields.

Public Fields are evil

Change this:

public string dataNombreCompleto;
public int dataIdUsuario;

To this:

public string dataNombreCompleto { get; set; }
public int dataIdUsuario { get; set; }
2
votes

You need to set the exact name of the displayMember. Try this

cbAdministrativos.DisplayMember = "dataNombreCompleto";
2
votes

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;

Here are the differences between Display Member and Value Member:

  • For DisplayMember property, it is designed to display strings in the comboboxes. ValueMember is hidden behind DisplayMember.
  • For 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:

2
votes

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.

1
votes

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.

0
votes

Had similar problem. Try to assign the DataSource first:

        cbAdministrativos.DataSource = usuarios;
        cbAdministrativos.DisplayMember = "dataNombre";
        cbAdministrativos.ValueMember = "dataIdUsuario";