Any postback can throw an exception connected to the Bind/Eval way of binding -
'Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control'.
The better way is by using customized code during processing the ItemUpdating event:
<asp:TemplateField HeaderText="Header text">
<ItemTemplate>
<asp:DropDownList ID="ddlItem"
runat="server" OnDataBound="ddlItems_DataBound"
DataSourceID="ObjectDataSource1" AppendDataBoundItems="false"
DataTextField="Name" DataValueField="Id" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
protected void ddlItems_DataBound(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow gvr = (GridViewRow)ddl.NamingContainer;
if (gvr.DataItem != null)
{
string id = ((DataRowView)gvr.DataItem)["Id"].ToString();
ddl.ClearSelection();
ListItem li = ddl.Items.FindByValue(id);
if (li != null) li.Selected = true;
}
}
It is important to use cast of sender, not direct calling to the GridView1 control. Every GridViewRow is actually DetailsView object.
protected void GridView1_RowUpdating
(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddl =
(DropDownList)(((GridView)sender).Rows[e.RowIndex].FindControl("ddlItems"));
e.NewValues["Id"] = ddl.SelectedValue;
}