How to append two field values in combobox display member in C#

后端 未结 7 668
死守一世寂寞
死守一世寂寞 2020-12-30 02:43

In my table, I have a field of firstname and lastname, now what I want is to set firstname and lastname as displaym

7条回答
  •  轮回少年
    2020-12-30 03:11

    This example will guide you how to do that without modifying your base class.

    First, you can leave your DisplayMember with one property, let's say:

    cmbEmployees.DisplayMember = "lastname";
    

    Now, 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. You will see this:

    private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
    {
    
    }
    

    And now write these following lines inside:

    private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
    {
        // Assuming your class called Employee , and Firstname & Lastname are the fields
        string lastname = ((Employee)e.ListItem).Firstname;
        string firstname = ((Employee)e.ListItem).Lastname;
        e.Value = lastname + " " + firstname;
    }
    

    That's it ;)

提交回复
热议问题