How to create a Winforms Combobox with Label and Value?

I can think of a few ways:

  • override the ToString() of a City class to return Name + "https://stackoverflow.com/" + Id;
  • ditto, but with a TypeConverter
  • add a DisplayText property that return the same, and use DisplayMember
  • build a shim class for the data

For the last:

var data = cities.Select(city => new {
     Id = city.Id, Text = city.Name + "https://stackoverflow.com/" + city.Id }).ToList();
cbo.ValueMember = "Id";
cbo.DisplayMember = "Text";
cbo.DataSource = data;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)