繁体   English   中英

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

[英]Gridview databound drop down list and updating

  • .NET 4.51,正在使用C#连接器从mySQL数据库检索数据
  • 我正在尝试将数据绑定下拉列表与网格视图一起使用。 我可以填充下拉列表,但无法弄清楚如何在OnRowUpdating中更新基础数据集中的值

网格:

<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>

并填充下拉列表:

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;
            }

         }
    }
}

然后更新所选值:

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;
     }
 }

但是在上面的dr总是NULL? 那么,我该怎么做才能获取记录中的“类型”字段以使用下拉列表中的值进行更新?

感谢帮助。

DataItem属性仅在GridView控件的RowDataBound事件期间和之后才可用。

根据文档,在OnRowUpdating事件上, DataItem将为null

尝试下面

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

您在dr中获得了空值,因为在OnRowUpdating事件中DataItem将为空,这与@Damith上面说的相同。为什么为空,您可以在此处观看。

试试吧

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;
    }
}

有关更多信息,请参见NewValues文档。

希望对您有帮助。

暂无
暂无

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

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