Winforms ComboBox DataBinding DisplayMember to SubObject Property

10,870

Just added to your code one method (event actually), and it's working.

public partial class Form1 : Form
{
    private List<UserDataModel> userData = new List<UserDataModel>();
    public Form1()
    {
        InitializeComponent();

        MyInit();
    }

    public void MyInit()
    {
        var userDataModel1 = new UserDataModel();
        userDataModel1.Name = "Mike";
        userDataModel1.Phone = "555-666";
        userDataModel1.Home = new HomeDataModel();
        userDataModel1.Home.StreetName = "MikeStreet";
        userDataModel1.Home.GeoLocationX = 111;
        userDataModel1.Home.GeoLocationY = 222;

        var userDataModel2 = new UserDataModel();
        userDataModel2.Name = "Jonathan";
        userDataModel2.Phone = "777-888";
        userDataModel2.Home = new HomeDataModel();
        userDataModel2.Home.StreetName = "JonathanStreet";
        userDataModel2.Home.GeoLocationX = 333;
        userDataModel2.Home.GeoLocationY = 444;

        userData.Add(userDataModel1);
        userData.Add(userDataModel2);

        // This works as usually:
        /*
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;
        */

        // But this works not (either with comboBox1.DataBindings.Add() nor with BindingSource):
        comboBox1.DisplayMember = "Home.StreetName";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;

        // To drive me crazy, THAT shit works:
        textBox1.DataBindings.Add("Text", userData, "Home.StreetName");

        /*
        So how can i use a String-Property of a SubObject as ComboBox-DisplayMember ???

        BTW: To rebuild the sample, you only need a normal Forms Application and
        then drop a ComboBox and a TextBox on it. Copy that code here, and run it.
        */
    }

    // To add this method - follow to my instructions below
    private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
    {
        e.Value = ((UserDataModel)e.ListItem).Home.StreetName;
    }
}

internal sealed class UserDataModel
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public HomeDataModel Home { get; set; }
}

internal sealed class HomeDataModel
{
    public string StreetName { get; set; }
    public int GeoLocationX { get; set; }
    public int GeoLocationY { get; set; }
}

To create this method (event), go to your form in a [Design] mode, right click on the ComboBox -> Properties.

In the top of the Properties window, click on Events (lightning icon),

look for Format in the events list below (under Property Changed) and type there some event name, let's say: ComboBoxFormat , and press Enter. That's it ;)

Share:
10,870
MBODM
Author by

MBODM

Updated on July 20, 2022

Comments

  • MBODM
    MBODM almost 2 years

    i searched for 2 hours or more and can not find an answer. So i try it here:

    I want to know how (and if it can be done at all) can i databind a List of Models to a WinForms ComboBox, and use a Property of a Property of the Model (thats in the List) as DisplayMember ? See Code here:

    public partial class Form1 : Form
    {
        private List<UserDataModel> userData = new List<UserDataModel>();
    
        public Form1()
        {
            InitializeComponent();
    
            MyInit();
        }
    
        public void MyInit()
        {
            var userDataModel1 = new UserDataModel();
            userDataModel1.Name = "Mike";
            userDataModel1.Phone = "555-666";
            userDataModel1.Home = new HomeDataModel();
            userDataModel1.Home.StreetName = "MikeStreet";
            userDataModel1.Home.GeoLocationX = 111;
            userDataModel1.Home.GeoLocationY = 222;
    
            var userDataModel2 = new UserDataModel();
            userDataModel2.Name = "Jonathan";
            userDataModel2.Phone = "777-888";
            userDataModel2.Home = new HomeDataModel();
            userDataModel2.Home.StreetName = "JonathanStreet";
            userDataModel2.Home.GeoLocationX = 333;
            userDataModel2.Home.GeoLocationY = 444;
    
            userData.Add(userDataModel1);
            userData.Add(userDataModel2);
    
            // This works as usually:
            /*
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Home";
            comboBox1.DataSource = userData;
            */
    
            // But this works not (either with comboBox1.DataBindings.Add() nor with BindingSource):
            comboBox1.DisplayMember = "Home.StreetName";
            comboBox1.ValueMember = "Home";
            comboBox1.DataSource = userData;
    
            // To drive me crazy, THAT shit works:
            textBox1.DataBindings.Add("Text", userData, "Home.StreetName");
    
            /*
            So how can i use a String-Property of a SubObject as ComboBox-DisplayMember ???
    
            BTW: To rebuild the sample, you only need a normal Forms Application and
            then drop a ComboBox and a TextBox on it. Copy that code here, and run it.
            */
        }
    }
    
    internal sealed class UserDataModel
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public HomeDataModel Home { get; set; }
    }
    
    internal sealed class HomeDataModel
    {
        public string StreetName { get; set; }
        public int GeoLocationX { get; set; }
        public int GeoLocationY { get; set; }
    }
    
  • MBODM
    MBODM over 11 years
    Hi Derek, i tried that solution too. and it compiled and ... worked. but not really, because my combobox1 shows only 1 dropdown-field: "Sesame Street". comboBox1.Items.Count is also 1. So it doesn´t really work. Why ? No idea...
  • MBODM
    MBODM over 11 years
    hi, has nothing to do (i think) with the bindingcontext. i tried this, to really be sure, combobox uses the forms bindingcontext anyway (and it does): < code see below in my own answer (cause of no code formatting in comments here) >
  • Derek
    Derek over 11 years
    Apologies, I'll look at this again.
  • Derek
    Derek over 11 years
    The Issue is with the HomeDataModel. Could you not include its three properties as part of the UserDataModel class? I think this would resolve the issue.
  • MBODM
    MBODM over 11 years
    Thanks for your kind efforts, Derek ! Clearly, i CAN add the properties to the UserDataModel, but THAT harms the base of my question :) And i also have many other ComboBoxes that need DisplayMember of SubProperty. If i put all that classes into one, would you call this a "good software design" ? :)
  • Derek
    Derek over 11 years
    I had no concept of the rest of your application. @Marcel. Its an interesting problem. I will look at it again, at some point today.