In my application, when I edit a row in gridview, I select some new data from the drop-down list.
I populate the dropdown menu as follows:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList emailsListDL = (DropDownList)e.Row.FindControl("emailsDL"); emailsListDL.DataSource = allUsersEmails;
But when I click the Refresh button from the template and enters the "RowUpdating" event, the selected value from the drop-down list happens every time the first value from this drop-down list.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { DropDownList emailsListDL = (DropDownList)GridViewAdvertisers.Rows[e.RowIndex].FindControl("emailsDL"); string email = emailsListDL.SelectedValue;
Does anyone have any ideas?
I tried many ways to set the selected value in the "RowDataBound" event, but no luck. I tried this:
1. emailsListDL.SelectedIndex = emailsListDL.Items.IndexOf(emailsListDL.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "OwnerMail").ToString())); 2. emailsListDL.SelectedValue = GridViewAdvertisers.DataKeys[e.Row.RowIndex]["OwnerMail"].ToString(); 3. emailsListDL.SelectedValue = GridViewAdvertisers.Rows[e.Row.RowIndex].Cells[1].Text; //ownerMail is a string (object) from the list of objects that I put as datasource to the gridview
Thanks Jeff
Update
My element template from aspx page:
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" ItemStyle-Width="150px" HeaderText="Owner Email" HeaderStyle-HorizontalAlign="Left" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="#e1e1e1"> <ItemTemplate> <asp:Label ID="LabelEmail" runat="server" Text='<%# Bind("OwnerMail")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="emailsDL" runat="server" Width="150"> </asp:DropDownList> </EditItemTemplate> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="180px" BorderWidth="1px" BorderColor="#e1e1e1"></ItemStyle> </asp:TemplateField>
source share