0

I have a ComboBox control which is set to DropDownList as DropDownStyle. The control is initialed like below;

List<Sample> samples = new List<Sample>(){
    new Sample(),//Empty record
    new Sample(){Id = 1, Name = "Sample1"}, 
    new Sample(){Id = 2, Name = "Sample2"}};

// Bind the ComboBox to the list
this.comboBox1.DataSource = samples;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";

When I set, this.comboBox1.Text = "Sample3"; the drop down doesn't show anything in the text area. It's obvious that Sample3 is not in the underlying DataSource which is samples list.

My requirement is to show an item which not in the drop down, but the DropDownStyle set as DropDownList. Is it possible?

This is possible when using a ComboBox that has a DropDownStyle set to DropDown.

Note: How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item? is not related to the question because OP is using ComboBox that has a DropDownStyle set to DropDown.

Community
  • 1
  • 1
Irshad
  • 3,071
  • 5
  • 30
  • 51

1 Answers1

0

You can achieve this concept by following code:

 DataTable dt = (DataTable)comboBox1.DataSource;
        DataRow dr = dt.NewRow();
        dr["Name"] = "Select";
        dr["ID"] = 0;

        dt.Rows.InsertAt(dr, 0);
        comboBox1.SelectedIndex = 0;
  • Please see comments in the Question. I can't change the `DataSource`. If I can then no need for this question. – Irshad Mar 11 '16 at 04:49