0

How would I bind a custom object which has got value and text to bind to a drop down in a property grid?

For example, I have a custom list like below:

List < Employee > _employeeList;
public class Employee {
    int employeeId;
    string employeeName;
}

I am able to bind a list of string to the dropdown in Property grid but not list of custom object.

Milo
  • 3,365
  • 9
  • 30
  • 44
  • 1
    Can you show how you are binding list of string to the dropdown? – Fabio Sep 19 '19 at 05:22
  • 1
    This link might help you .https://stackoverflow.com/questions/2251075/winforms-data-binding – Praveen M Sep 19 '19 at 05:24
  • The answer of Oliver Rogier should help you. An other option is to override the `ToString()` method where you compose a string that combines both properties the way you want to display them. For instance: `employeeName + " - " + employeeId`. Or: `string.Format("{0} ({1})", employeeName, employeeId);` By default the `ToString()` method is called by the control. – Stefan Sep 19 '19 at 06:12

2 Answers2

0

Typically you set the combo's DataSource to the list, the DisplayMember to a string of the name of the property to display and the ValueMember to a string of the name of the property to use as the value (when calling for selectedValue):

public class Person{
  public string Name { get; set; }
  public int Id { get; set; }
}
var peeps = new List<Person>(){
  new Person(){ Name="John",Id=1 },
  new Person(){ Name="Jane",Id=2 }
};
combo.DataSource = peeps;
combo.DisplayMember = "Name";
combo.ValueMember = "Id";

Chose "Jane" in the combo, and (int)combo.SelectedValue will be 2

If you don't set the display and value members, the combo will just call ToString on every item in the list and use it as the display text, and the whole object as the value. Without an overridden ToString the default is just to return the type Name. You're hence probably seeing a list full of "YourNamespace.Person" or equivalent

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You should use public properties, not private fields:

public class Employee
{
  public int EmployeeId {get; set; }
  public string EmployeeName {get; set; }
}
  • Public to be used outside of the class.
  • Properties with at least a getter to be suitable with OO design.

Using public properties may solve the problem that is caused by using private fields as bindings use only public properties.

You should also implement ToString(), such as:

public override string ToString()
{
  return EmployeeName;
}

This let controls to display a string representation of objects.

  • @caius-jard Why a comment and not an answer? –  Sep 19 '19 at 05:56
  • Because it doesn't answer the question, it merely points out some small fact about c# convention on publicly accessible variables vs properties - changing a public string to a public property won't suddenly make his binding problem work out, and that is the question being asked. It's like someone posting up *"Why doesn't my code to add two numbers give the right result - `string summ = "2" + "2";` - it's giving 22, not 4 "* and you post an answer of "You spelled SUM wrong". -> true, but not an answer to the question – Caius Jard Sep 19 '19 at 06:01
  • 1
    @caius-jard But using public properties may solve the problem that is caused by using private fields : bindings use only public properties, from what I know. –  Sep 19 '19 at 06:07
  • This is true, but the way the "answer" was originally put didn't include this info, and there was no indication at the time that this was even the problem - attempting to bind to something that is not a property gives an exception. Had you included more supporting information like this in the original answer then it would have been less of a comment and more of a guess at what might be wrong- closer to an answer. As it stands, your later edit regarding adding an override of ToString makes this an answer but the lecture on c# conventions is still only a comment – Caius Jard Sep 19 '19 at 06:45
  • 2
    @caius-jard I added explanation about bindings behavior to the answer, thanks. –  Sep 19 '19 at 07:35