我正在尝试检查数据库中是否有记录的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之间匹配的产品。为什么是这样?
我相信解决您的问题的最佳方法是确定是否CheckBox
应在对“ gvProduct”控件进行数据绑定之前对其进行检查。Data Source
提前设置该值将防止您GridView
以后必须重新访问。
如果这不是一个选择,则下面的解决方案将起作用,但由于您必须重新访问GridView
,然后迭代其Row集合,因此更加笨拙。
免责声明,是在没有VS2012的情况下完成的,因此,请原谅任何较小的语法错误。我修改了您的代码,以删除与该解决方案无关的部分。首先,获取两个对象列表:
List<ProductPacking> prodList = prodPackBLL.getAllProductByCategory(category);
List<DistributionStandardPackingUnitItems> distSPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);
接下来,您可以使用简单的LINQ查询来比较对象列表,以查找ProductPacking
其name属性等于distSPUItemList中的项目的对象。这将创建一个新的仅匹配对象的集合:
var available = // Start with prodList
from a in prodList
// perform an inner-join between prodList and distSPUItemList only returning objects whose names match
// (x => x.name == a.name) compares b.name to a.name, this is specifically what enforces that names match.
from b in distSPUItemList.Where(x => x.name == a.name)
// after inner joining is done, only select objects from prodList (we could select more things, but we only need these.)
select a;
最后,您可以迭代gridview行,并查看GridView中显示的name属性是否在可包装的产品列表中,如果是,则选中该复选框。
GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
foreach (GridViewRow gr in gvForCheckBox.Rows)
{
// query the available collection to see if it contains a ProductPacking object with a name equal to what is in the current GridView row.
// I would recommend to match on a PK
// disclaimer, matching on name may be a problem if two different products can have the same name.
if (available.Where(x=>x.Name==gr.Cells[1].Text).Any())
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbSelect");
cb.Checked = true;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句