Get dropdown value in GridView while in Edit Mode RRS feed

  • Question

  • User-2051535749 posted

    I have a gridview that when the user clicks [Edit], I populate a dropdown list. The dropdown is populated in code behind in the RowDataBound() method. When the users makes a selection I want to capture that value in my update method.  In the update method, I need to get the selected value and I'm unable to find the dropdown control.

    How can I get the selected value of the drop down when in Edit Mode and from my update method?


    <asp:GridView ID="GridView" runat="server" DataKeyNames="Id"  
            AutoGenerateColumns="False" DataSourceID="GridViewDataSource2" GridLines="None"
            AllowPaging="True" PageSize="25" OnRowDataBound="GridView_RowDataBound" OnRowUpdated="RowUpdated"> 
            <asp:CommandField HeaderText="Edit" ButtonType="Image" CancelImageUrl="~/images/cancel.gif" EditImageUrl="~/images/edit.gif" ShowEditButton="True" UpdateImageUrl="~/images/save.gif">
                <ItemStyle CssClass="GridViewEdit" />
            <asp:BoundField DataField="ManufactureName" HeaderText="Manufacturer" SortExpression="Manufacturer" />
            <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Description">
                     <%#Eval("Manufacturer").ToString() %>
                     <asp:TextBox ID="EditManu" runat="server" Text='<%# Bind("Manufacturer") %>'></asp:TextBox>
                <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Category">
                    <asp:Label ID="category" runat="server" Text='<%#Eval("PrimaryCategory").ToString() %>'></asp:Label>
                        <asp:DropDownList ID="Category" runat="server"></asp:DropDownList>

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
                if(e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
                    DropDownList MainCategory = (DropDownList)e.Row.FindControl("Category");
                    Cat1.DataSource = GetManufacturers.Categories();
                    Cat1.DataTextField = "ManuName";
                    Cat1.DataValueField = "ManuId";
    protected void RowUpdated(object sender, GridViewUpdatedEventArgs e)
    	    Manufacturer.Details manu =  new Manufacturer.Details();	
                DropDownList Cat1 = (DropDownList)e.Row.FindControl("Category");
                // get error object reference not set to an instance of an object
                manu.Category = Cat1.SelectedItem.Value.ToString();

    Friday, October 5, 2018 7:09 PM

All replies

  • User-1716253493 posted

    Use rowupdating event instead, based gv editindex you can find the control

    But updated event, editindex is -1

    Saturday, October 6, 2018 1:51 PM
  • User-1171043462 posted

    Sunday, October 7, 2018 8:02 AM
  • User-2051535749 posted

    I looked at this prior to posting and I get an 500 error on this: 

     string selectedCity = DataBinder.Eval(e.Row.DataItem, "City").ToString();
                        ddlCities.Items.FindByValue(selectedCity).Selected = true;

    Exception message: Object reference not set to an instance of an object.

    and the selected value isn't being saved, how can I get that? 

    this returns nothing:

    protected void EditCustomer(object sender, GridViewEditEventArgs e)
        gvCustomers.EditIndex = e.NewEditIndex;

    Monday, October 8, 2018 11:50 AM
  • User-2051535749 posted

    I got it working, I had to create a hidden field, and populate that field when the text changed in the dropdown. 

    Monday, October 8, 2018 1:33 PM
  • User-1171043462 posted

    If my post helped. Pls mark Answer

    Monday, October 8, 2018 1:48 PM