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