开发者

Remove Default Selection from a DataBound ComboBox

开发者 https://www.devze.com 2023-03-28 06:59 出处:网络
I have data bind a combo box with a list of values in the database, now, the first record is bein开发者_开发问答g displayed as a default value, i need to change this and set to blank or my custom mess

I have data bind a combo box with a list of values in the database, now, the first record is bein开发者_开发问答g displayed as a default value, i need to change this and set to blank or my custom message, any solution?


You can also set the Text property of the ComboBox directly to get a custom message, such as:

comboBox1.SelectedIndex = -1;
comboBox1.Text = "Select an item";


Adding an empty item as the first item to the DataSource before binding is the ugly fix for this issue.

Setting the SelectedIndex as -1 might help.

Below is another option, but you may have to validate when retrieving the selected item.

comboBox.Text = string.Empty; 


After binding data to your combobox, insert new item at index 0:

combobox1.Items.Insert(0, "Default Value"); 

or

combobox1.Items.Insert(0, ""); //Empty
0

精彩评论

暂无评论...
验证码 换一张
取 消