Reputation: 2419
Here is my aspx code:
<EditItemTemplate>
<asp:DropDownList ID="ddlTotalColumn" runat="server">
<asp:ListItem Value="">Select value</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
My aspx.cs code:
protected void gvTest_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow selected_row = gvTest.Rows[e.RowIndex];
var total_column_drop_down_list = (DropDownList)selected_row.FindControl("ddlTotalColumn");
int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value);
gvTest.EditIndex = -1;
...
}
At this line: int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value); I have an error: "Input string was in incorrect format" because "total_column_drop_down_list.SelectedItem.Value" will return empty string ("").
So is there any bright idea?
Upvotes: 2
Views: 3303
Reputation: 11433
It sounds like you have made the classic mistake of not putting your databinding code inside a if (!Page.IsPostBack)
block. Thus, your GridView re-binds, and you get default values in your RowUpdating event (rather than what you selected).
Wherever you are binding your GridView, in Page_Load for instance, you need to do this:
if (!Page.IsPostBack)
{
BindGrid();
}
Where "BindGrid()" is whatever code you call to databind your GridView.
On a slightly unrelated note, you can actually use the GridViewUpdateEventArgs parameter that's passed to that method to grab the updated values (rather than using FindControl to get the DropDownList, and then getting the values).
Upvotes: 4