c# - How to count rows in a DataView

DataView Count Property
.Net framework's DataView represents a databindable, customized view of a DataTable for sorting, searching, filtering, navigation and editing. DataView object does not store data but it represents a connected view of its corresponding DataTable.

DataView RowFilter property allow us to get or set the expression used to filter which rows are viewed in the DataTable. DataView RowStateFilter property allow us to get or set the row state filter used in the DataView.

DataView Count property allow us to get the number of records (rows) in a DataTable after RowFilter and RowStateFilter have been applied. Count property value data type is System.Int32. this integer value represents the number of records in the DataView. Count property implements as ICollection.Count.

The following ado.net c# example code demonstrate us how can we count a DataView's rows (records) programmatically at run time in an asp.net application.
DataViewCountProperty.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;
        dc1.Unique = true;

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "Category";
        dc2.DataType = typeof(string);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "BookName";
        dc3.DataType = typeof(string);

        DataColumn dc4 = new DataColumn();
        dc4.ColumnName = "Author";
        dc4.DataType = typeof(string);

        dt.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3, dc4 });

        dt.Rows.Add(new object[] { 1, ".NET", "ASP.NET jQuery Cookbook", "Sonal Aneel Allana" });
        dt.Rows.Add(new object[] { 2, "Flash", "Joomla! with Flash", "Suhreed Sarkar" });
        dt.Rows.Add(new object[] { 3, ".NET", "ASP.NET 3.5 Application Architecture and Design", "Vivek Thakur" });
        dt.AcceptChanges();

        GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();

        //this line create a new DataView
        DataView dView = new DataView(dt);
        dView.RowFilter = "Category = '.NET'";

        Label1.Text = "Here we create a new DataView<br />" +
                      "and set the RowFilter expression (Category = '.NET')";

        int totalRows = dView.Count;
        Label1.Text += "<br /><br />Total Rows in this DataView: " + totalRows.ToString();
        GridView2.DataSource = dView;
        GridView2.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataView Count property to get number of records (rows) after RowFilter in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataView Count property to get
            <br /> number of records (rows) after RowFilter in ado.net
        </h2>
        <hr width="575" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="DarkOrange" Height="30" />
            <RowStyle BackColor="BlueViolet" />
            <AlternatingRowStyle BackColor="Violet" />
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:GridView 
            ID="GridView2"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="DarkOrange" Height="30" />
            <RowStyle BackColor="BlueViolet" />
            <AlternatingRowStyle BackColor="Violet" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples