简体   繁体   English

Gridview数据绑定下拉列表和更新

[英]Gridview databound drop down list and updating

  • .NET 4.51, data is being retrieved from a mySQL database using the C# connector .NET 4.51,正在使用C#连接器从mySQL数据库检索数据
  • 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 我可以填充下拉列表,但无法弄清楚如何在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? 但是在上面的dr总是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. DataItem属性仅在GridView控件的RowDataBound事件期间和之后才可用。

according to documentation, on OnRowUpdating event DataItem will be null 根据文档,在OnRowUpdating事件上, DataItem将为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 . 您在dr中获得了空值,因为在OnRowUpdating事件中DataItem将为空,这与@Damith上面说的相同。为什么为空,您可以在此处观看。

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 有关更多信息,请参见NewValues文档。

I hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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