简体   繁体   中英

Gridview databound drop down list and updating

  • .NET 4.51, data is being retrieved from a mySQL database using the C# connector
  • I am attempting to use a data bound drop down list with a grid view. I can get the drop down list populated but cannot figure out how to update the value in the underlying dataset in OnRowUpdating

The grid:

<asp:GridView ID="grdProductPrices" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="ID" DataSourceID="dscProductPriceMaintenance" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" 
                CssClass="table table-bordered table-condensed table-hover table-striped" EnableTheming="True" PageSize="30"
                OnRowDataBound="grdProductPrices_OnRowDataBound"    
                OnRowUpdating="grdProductPrices_OnRowUpdating"
                >
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="ID" SortExpression="ID">
                        <ItemTemplate>
                            <asp:Label ID="lblProductID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type">
                        <EditItemTemplate>
                            <asp:Label ID="lblProductType" runat="server" Text='<%# Bind("Type") %>' Visible = "false" />  
                            <asp:DropDownList ID="ddlProductType" runat="server" CssClass="form-control"></asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProductType" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerTemplate>
                    <nuc:GridPager ID="gridPager" runat="server"
                        ShowFirstAndLast="True"
                        ShowNextAndPrevious="True"
                        PageLinksToShow="10"
                        Position="Right"
                        NextText="Next"
                        PreviousText="Prev"
                        FirstText="First"
                        LastText="Last" />
                </PagerTemplate>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:SqlDataSource ID="dscProductPriceMaintenance" runat="server" ConnectionString="<%$ ConnectionStrings:mysqlConn %>" ProviderName="<%$ ConnectionStrings:mysqlConn.ProviderName %>"
        DeleteCommand="DELETE FROM Product WHERE ID = ?"
        InsertCommand="INSERT INTO Product (ID, Name, Variety, Unit, Price) VALUES (?ID, ?Name, ?Variety, ?Unit, ?Price)"
        SelectCommand="SELECT ID, Comments, Name, Variety, Unit, Price, Type, Available, List_Product FROM Product"
        UpdateCommand="UPDATE Product SET Name = ?Name, Variety = ?Variety, Unit = ?Unit, Price = ?Price, Comments = ?Comments, Available = ?Available, List_Product = ?List_Product WHERE ID = ?ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int64" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Available" Type="Int32" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="ID" Type="Int64" />
            <asp:Parameter Name="List_Product" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Price" Type="Single" />
            <asp:Parameter Name="Unit" Type="String" />
            <asp:Parameter Name="Variety" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Available" Type="Int32" Direction="Input" />
            <asp:Parameter Name="Comments" Type="String" Direction="Input" />
            <asp:Parameter Name="ID" Type="Int64" Direction="Input" />
            <asp:Parameter Name="List_Product" Type="Int32" Direction="Input" />
            <asp:Parameter Name="Name" Type="String" Direction="Input" />
            <asp:Parameter Name="Price" Type="Single" Direction="Input" />
            <asp:Parameter Name="Unit" Type="String" Direction="Input" />
            <asp:Parameter Name="Variety" Type="String" Direction="Input" />
        </UpdateParameters>
    </asp:SqlDataSource>

and to populate the drop down list:

protected void grdProductPrices_OnRowDataBound(object aSender, GridViewRowEventArgs aEventArgs)
{
    if (aEventArgs.Row.RowType == DataControlRowType.DataRow)
    {
        //Populate the drop down list
        DropDownList cmbProductTypes = aEventArgs.Row.FindControl("ddlProductType") as DropDownList;
        if (cmbProductTypes != null)
        {
            IQueryable<ProductType> productTypes = BusinessLayer.Instance()
                                                    .ProductTypesGet();

            cmbProductTypes.DataTextField = "Name";
            cmbProductTypes.DataValueField = "ID";
            cmbProductTypes.DataSource = productTypes;
            cmbProductTypes.DataBind();

            //Set the selected index
            Label label = aEventArgs.Row.FindControl("lblProductType") as Label;
            if (label != null)
            {     
               string productTypeId = label.Text;
               cmbProductTypes.Items.FindByValue(productTypeId).Selected = true;
            }

         }
    }
}

and then to update the selected value:

protected void grdProductPrices_OnRowUpdating(object aSender, GridViewUpdateEventArgs aEventArgs)
{
     DropDownList cmbProductTypes = grdProductPrices.Rows[aEventArgs.RowIndex].FindControl("ddlProductType") as DropDownList;
     if (cmbProductTypes != null)
     {
         DataRowView dr = grdProductPrices.Rows[aEventArgs.RowIndex].DataItem as DataRowView;
         if (dr != null) dr["Type"] = cmbProductTypes.SelectedValue;
     }
 }

However in the above the dr is always NULL? So what do I have to do to get the "Type" field in the record to update with the value from the drop down?

Appreciate the help.

The DataItem property is only available during and after the RowDataBound event of a GridView control.

according to documentation, on OnRowUpdating event DataItem will be null

try below

e.NewValues["Type"] = cmbProductTypes.SelectedValue;

You are getting null value in dr because on OnRowUpdating event DataItem will be null same thing @Damith have said above.Why it is null you can watch Here .

Try it

protected void grdProductPrices_OnRowUpdating(object aSender, GridViewUpdateEventArgs aEventArgs)
{
    DropDownList cmbProductTypes = grdProductPrices.Rows[aEventArgs.RowIndex].FindControl("ddlProductType") as DropDownList;
    if (cmbProductTypes != null)
    {
        e.NewValues["Type"] = cmbProductTypes.SelectedValue;
    }
}

For more information refer NewValues Documentation

I hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM