c# - Gridview数据绑定(bind)下拉列表和更新

标签 c# mysql asp.net gridview

  • .NET 4.51,正在使用 C# 连接器从 mySQL 数据库检索数据
  • 我正在尝试使用带有 GridView 的数据绑定(bind)下拉列表。我可以填充下拉列表,但无法弄清楚如何更新 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?那么我需要做什么才能让记录中的“类型”字段使用下拉列表中的值进行更新?

感谢您的帮助。

最佳答案

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

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

尝试下面

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

关于c# - Gridview数据绑定(bind)下拉列表和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163166/

相关文章:

c# - 仅使用 SSL 进行身份验证

mysql - Mysql插入一行后输出主键

c# - 这里的语法有什么问题?它说服务器标签格式不正确

javascript - 当超出文本框限制时以视觉方式向用户指示

c# - Entity Framework 属性隐藏

c# - 在 Nullable<int> 列表上使用 Enumerable.OfType 删除空项

c# - 如何在 C# 中使用 XmlDsigC14NTransform 类

PHP/mysqli : Inserting IP address with mysqli_stmt_bind_param()

PHP - 将日期插入mysql

javascript - Angular.js 的 ASP.NET 捆绑和缩小错误