Sometimes, after the results returned from the database are associated with the ComboBox, you also need to add items such as "Others" or "all.
You can create a new listitem on the web, but winform does not.
You can add a datarow in datatable to implement the same function.
String sequence string = "select ID, name from staff"; <br/> try <br/>{< br/> sqldatabase SDB = new sqldatabase (); <br/> SDB. open (); <br/> datatable dtbl = SDB. executedatatable (optional string); <br/> SDB. close (); <br/> // Add the "all" option <br/> datarow DR = dtbl. newrow (); <br/> Dr ["ID"] = dbnull. value; <br/> Dr ["name"] = "all"; <br/> dtbl. rows. insertat (DR, 0); <br/> cbostaff. datasource = dtbl; <br/> bostaff. selectedindexchanged + = new eventhandler (cbostaff_selectedindexchanged); <br/>}< br/> catch <br/>{< br/>}
The staff table in the database contains two fields: ID and name. The result table returned by the query is dtbl,
Dtbl. newrow () generates a new datarow, and then sets the IDs and names of the two Dr fields to dbnull. Value and "all" respectively ".
Dtbl. Rows. insertat () inserts datarow at the beginning of the list. You can also insert it to the end of the list.
Then cbostaff. datasource = dtbl is enough.