Problem with ComboBox control plus default option in WinForm

Source: Internet
Author: User

WinForm programming, Label,textbox,combobox and other controls are almost the most used, in the design often encounter some small problems, such as: ComboBox control after binding the data source, how to set the default value?


This is the case after the ComboBox binds the data source
BEIJING <----The default is this one.
Shanghai
Shenzhen
....
Want to make it appear like this
Please select City <--------to show this by default
Beijing
Shanghai
Shenzhen

....


Try the procedure:

1. Modifying the DropDownStyle attribute also does not work.

2, eliminate the data source directly with the ComboBox. The Items.Add () method seems to be possible, but it loses the SelectedValue binding Id,selecteditem binding name function, because the storage ID is not a name when it is saved. So it's not going to work that way.


3, if it is the DropDownList control in ASP., after binding the data source, presumably this will be done DropdownList1.Items.Insert (0,new ListItem ("name", "value"); But the ComboBox is not set up.


Finally, in this way:

The data source is loaded into the DataTable object, and a new row is constructed using the datatable data structure and inserted into the first item.

The sample code is as follows:

Manipulate the data layer class and modify it according to your actual code
CLSCHECKITEMOPR ITEMBLL = new Clscheckitemopr ();

Get a DataTable DataSet
DataTable dtbl = itembll.getasdatatable ("Islock=false", "Syscode", 1);

Insert a default option
DataRow dr = Dtbl. NewRow ();
dr["ItemCode"] = "0";//This value can be set by itself, but not with the existing ID duplicate, so it is better to set a special point
dr["ItemName"] = "Please select City";
Dtbl. Rows.insertat (DR, 0);//Specify the starting position to insert

Cmbcheckitem.datasource = dtbl;//is bound to a ComboBox control with a DataTable

Cmbcheckitem.valuemember = "ItemCode";//Set SelectedValue field
Cmbcheckitem.displaymember = "ItemName";//Fields displayed in the interface

Problems with the ComboBox control plus default options in WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.