如何使用linq在有条件的情况下对gridview中的行进行计数? [英] How to use linq to count rows in gridview with where conditions ?

查看:145
本文介绍了如何使用linq在有条件的情况下对gridview中的行进行计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有未绑定的checkbos和BoundFields的网格视图,我该如何编写查询以对所有行进行计数,其中
checkbox (cbx)==checked and boundfield "Tytle"=="household"
gridvew看起来像:

Hi,
I have a grid view with unbound checkbos and BoundFields how do I write the query to count all the rows where
checkbox (cbx)==checked and boundfield "Tytle"=="household"
the gridvew looks like:

<asp:GridView ID="gvPerson" runat="server" AutoGenerateColumns="False"

    DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
        <asp:CheckBox id="cbx" runat="server"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"

            InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Gender" HeaderText="Gender"

            SortExpression="Gender" />
    </Columns>

推荐答案

如果是我,我将处理复选框click事件,并在绑定到网格的实际对象中设置bool IsChecked属性,然后您可以在对象本身上简单地使用Linq.

If it were me, I''d handle the checkbox click event, and set a bool IsChecked property in the actual object bound to the grid, and then you could simply use Linq on the object itself.

var count = (from item in itemCollection
             where item.IsChecked && item.Tytle == "household"
             select item).Count();





That would make it much simpler.


IList<GridViewRow> rows = new List<GridViewRow>();
      foreach (GridViewRow item in gvPerson.Rows)
      {
          //Enter Condition Here
          rows.Add(item);
      }
      IEnumerable<GridViewRow> irows = rows.AsEnumerable<GridViewRow>();
      Response.Write(string.Format("Numeber of Rows Using LINQ:{0}", irows.Count()));


这篇关于如何使用linq在有条件的情况下对gridview中的行进行计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆