【发布时间】:2013-12-29 01:33:56
【问题描述】:
如果数据库中有记录,我正在尝试检查 CheckBox 中 GridView 中的某些行。假设我在一个类别中有产品 1、2、3、4,可用于包装的产品是 1 和 3。在我的 GridView 中,对于每个类别,我只选中了产品 1 和 3 的复选框,而不是所有该类别的产品。这是我设置GridView的方法:
<!-- Collapsible panel extender body -->
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
<asp:Label ID="lblBodyText1" runat="server" />
<!-- Grid view to show products based on each category -->
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="998px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="750px" />
<asp:BoundField DataField="categoryName" HeaderText="Category" />
<asp:BoundField DataField="inventoryQuantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
</asp:Panel>
从后面的代码中,我首先根据类别获取所有产品。我将其命名为 prodList。然后,我得到所有可用于包装的产品。我将其命名为 distSPUItemList。我遍历两个列表,如果它们的名称匹配,我得到行并选中复选框:
List<ProductPacking> prodList = new List<ProductPacking>();
//Get all products based on category
prodList = prodPackBLL.getAllProductByCategory(category);
gv.DataSource = prodList;
gv.DataBind();
List<DistributionStandardPackingUnitItems> distSPUItemList = new List<DistributionStandardPackingUnitItems>();
distSPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);
for (int i = 0; i < distSPUItemList.Count; i++)
{
for (int j = 0; j < prodList.Count; j++)
{
GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
foreach (GridViewRow gr in gvForCheckBox.Rows)
{
if (prodList[j].name == distSPUItemList[i].name)
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbSelect");
cb.Checked = true;
}
}
}
}
但是,对于CheckBox,它只检查该类别中的所有产品,而不是检查 prodList 和 distSPUItemList 之间匹配的产品。这是为什么呢?
【问题讨论】:
-
您的
ProductPacking类是否有一个布尔属性,表明它可用于打包?可以加一个吗?如果是这样,您可以在数据绑定期间设置复选框“已检查属性”。 -
不,不,我想要做的是得到一份产品清单。我还得到了一份标准包装单位物品的清单。如果产品在特定配送的标准包装单元项目列表中,则该产品的复选框将被选中。这就是为什么我通过两个列表来匹配名称
标签: c# asp.net linq gridview checkbox