add default to database populated dropdownbox
I have a dropdown list that is currently populated from a result set of a stored procedure. The datasource for the DDL is set to the resultset.
Without inserting a default record into the database (I don't want to do this) how can I add an item to the DDL which is the default selected i开发者_开发问答tem on form load?
Modify the stored procedure to include and union a dummy row.
(SELECT 1 as X, 'abc' AS Y) UNION (SELECT X, Y FROM your_table);
The other option is adding the "default" selection to the datasource before binding it to the combobox.
Something like this would be for a DataTable
DataRow row = dt.NewRow();
row["Id"] = 0;
row["Text"] = "Select...";
dt.Rows.InsertAt(row, 0);
精彩评论