user3766910
user3766910

Reputation: 55

How to display many column in one comboBox

I want to display many columns in one comboBox.

I've tried this:

 da = new SqlDataAdapter("select * from do_data",cn);
        da.Fill(dt3);
        comboBox1.DataSource = dt3;
        comboBox1.DisplayMember = "fname+lname";
        comboBox1.ValueMember = "id";

But it doesn't display both columns it displays the 'ValueMember'.

How to do it?

Upvotes: 2

Views: 102

Answers (2)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

You could add a computed column and set it as display member

dt3.Columns.Add("Combined", typeof(string), "fname+'  '+ fname");

comboBox1.DataSource = dt3;
comboBox1.DisplayMember = "Combined";
comboBox1.ValueMember = "id";

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try like this

da = new SqlDataAdapter("select id,fname + ' ' + lname As Name from do_data",cn);
da.Fill(dt3);
comboBox1.DataSource = dt3;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "id";

If you want all columns then try below

da = new SqlDataAdapter("select *,fname + ' ' + lname As Name from do_data",cn);
da.Fill(dt3);
comboBox1.DataSource = dt3;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "id";

Upvotes: 2

Related Questions