To have a dropdownlist inside a gridview, we use the property "SelectedValue='<%# Eval("datakeyfield") %>' knowing that it won't show in the intellisense. To handle null values, we can have an item with a value of null. The SQLDataSource for the dropdownlist is better off put outside the gridview, so that it does not get created as many times as the dropdownlist is.
Here's the code for the DropDownList Template Field :
The Dropdownlist is Filled From a table called Categories (see schema below)
<asp:TemplateField HeaderText="Category" SortExpression="CategoryID">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SourceCategories"
AppendDataBoundItems="true" DataTextField="CategoryName" DataValueField="CategoryID"
Width="190px" Enabled="false" SelectedValue='<%# Eval("CategoryID") %>'>
<asp:ListItem Text="No Item was Selected, to handle null values" Value="" />
In the code above, Notice the property SelectedValue, and the static Item to handle null values. to this, we add the AppendDataBoundItems Property, and the dropdownlist is ready.
Here's the complete code listing with a functional gridview:
The GridView is filled from a table Called Clients (see schema below)
Here's are the table definitions: (int and varchar(50) are the datatypes used)
Screen Shot for the gridview
Comments are welcome