gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:10:31 25 4
gpt4 key购买 nike

  • .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com