How to get the selected value from the drop-down list in gridview edit mode?

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;//list of strings with the emails of the users emailsListDL.DataBind(); } } } 

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; //the selected value is every time the first value from the dropdownlist } 

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> 
+4
source share
4 answers

The selected SelectedIndex value will always be 0 unless you define it in the DropDownList definition.

Edit: @Tim Schmelter should add his comment as anwer. In the meantime, I will paraphrase other readers: you need to check the feedback (see Comments above).

+1
source

You can declare ComboBox mainBX = new Combobox(); and you can declare an event

 private void onSeletedIndexChange(object sender,EventArgs e) { mainBX = (ComboBox)(sender); } 

and then you have to loop through the foreach ComboBox in the GridView and Add this event. Than all you have to do, take mainBX.seletedItem ();

I think this is what you need if you don't apologize.

0
source

To set the selected value to a RowDataBound -Event, you must do this as follows:

 (DropDownList)e.Row.Cells[/*index*/].Controls[/*index, or use FindControl*/]).Items.FindByValue(((DataRowView)e.Row.DataItem).Row.ItemArray[/*columnnumber*/].ToString()).Selected = true; 

FindByValue finds the current text value of the field in the "normal" view mode and sets the value to DropDownList with the value.

Of course, this must be concluded in

 if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { } 

As for your “getting value when updating” problem, I must honestly say that I have no clue, since your approach is exactly the same as mine, only the difference: my work.

Here is my code if it helps:

  protected void gridVariables_RowUpdating(object sender, GridViewUpdateEventArgs e) { string control = ((DropDownList)gridVariables.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedValue; gridVariables.EditIndex = -1; this.gridVariables_DataBind(control, e.RowIndex); } private void gridVariables_DataBind(string control, int index) { DataTable dt = (DataTable)Session["variableTable"]; //features a DataTable with the Contents of the Gridview dt.Rows[index]["ControlType"] = control; gridVariables.DataSource = dt; gridVariables.DataBind(); } 
0
source

I had the same problem with another solution, I implemented custom paging in GridView using a custom pager, and in this process the RowCommand method was added, which rewrote the grid with the new page index. How this happens, although the RowCommand method is also called during the upgrade.

So, be sure to check out any other locations that might be tied anywhere in your code. Hope this helps someone else.

  protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)
 {
     string commandName = e.CommandName;

     switch (commandName)
         {
             case "Page1":
                 HandlePageCommand (e);
                 // binding should happen here
                 break;
         }

         // this line is in the wrong location, causing the bug    
         BindGrid1 ();
 }
0
source

All Articles