peace
peace

Reputation: 887

Fill Combo-box via DataSource Using Values from Various Columns

            Employee emp = new Employee();
            comHandledBySQt.DataSource = emp.GetDataFromTable("1");
            comHandledBySQt.DisplayMember = "FirstName";

The above code displays drop list of employees first names in a combo box. I want first name and last name to be displayed. How can i do it?

I tried to include two columns FirstName and LastName as below but didn't work.

comHandledBySQt.DisplayMember = "FirstName" + " " + "LastName";

Any help will be appreciated.

Upvotes: 0

Views: 715

Answers (2)

Robert Schroeder
Robert Schroeder

Reputation: 2421

Easiest way would be to select an anonymous object with the values you wanted.

   Employee emp = new Employee();
   comHandledBySQt.DataSource = from x in emp.GetDataFromTable("1")
                    select new { x.Id, Name = x.FirstName + " " + x.LastName };
   comHandledBySQt.DisplayMember = "Name";
   comHandledBySQt.ValueMember = "Id";

Upvotes: 1

this. __curious_geek
this. __curious_geek

Reputation: 43207

If you need to do binding with the datasource straight away, you'll need to create a custom-view for this. Or you can iterate over rows returned from emp.GetDataFromTable() and add each row to DropDown with string.Format() or some string manipulation.

Upvotes: 1

Related Questions