Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display blank instead of first item in combobox

Code:

cmbItemType.DisplayMember = "Text";
cmbItemType.ValueMember = "Value";

var items = new[] {
        new { Text = "Text1", Value = "1"},  
        new { Text = "Text2", Value = "2"}
        };

cmbItemType.DataSource = items;

The above code displaying two items, but how to display a blank item in the field. Now at load, it is displaying Text1 without selecting. I want to display a blank item instead of Text1.

Note: Blank item should not be added to the list, so when selecting the combobox, I don't want to see a blank item above Text1 display text. Thanks.

like image 950
Programmer Avatar asked May 13 '15 06:05

Programmer


People also ask

How do you empty a combo box?

The easiest way to clear the second combobox is to just use Reset(). That will clear all the current selections and reset the combobox to its default selection.

How do I hide a ComboBox item in C#?

You can't "hide" it. You can only remove it. The problem is the source of the data the combo is using. If it's just a list of items you added to the combo, OK, just don't add the item you want to "hide".

How do I add options to my ComboBox?

To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control. Click the ellipses (...) button next to the Items property.

How do I make my combo box read only?

Or if you want it completely read only you can set Enabled = false , or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa. But please note: readonly !=


1 Answers

After you have set items as the DataSource:

cmbItemType.SelectedIndex = -1;
like image 169
Fung Avatar answered Oct 25 '22 09:10

Fung