在Gridview中检查复选框

时间:2013-12-29 01:33:56

标签: c# asp.net linq gridview checkbox

如果数据库中有记录,我试图检查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之间匹配的产品。这是为什么?

1 个答案:

答案 0 :(得分:0)

我认为解决问题的最佳方法是确定在数据绑定“gvProduct”控件之前是否应检查此CheckBox。提前在Data Source中设置此值将阻止您稍后重新访问GridView

如果这不是一个选项,下面的解决方案将起作用,但更加笨重,因为您必须重新访问GridView然后迭代其Row集合。

免责声明,在没有VS2012的情况下完成,所以请原谅任何轻微的语法错误。我修改了您的代码,以删除与此解决方案无关的部分。首先,获取两个对象列表:

List<ProductPacking> prodList = prodPackBLL.getAllProductByCategory(category);
List<DistributionStandardPackingUnitItems> distSPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);

接下来,您可以使用简单的LINQ查询来比较查找{name}属性等于distSPUItemList中项目的ProductPacking个对象的对象列表。这将创建仅匹配的对象的新集合:

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