Site icon ParallelCodes

DataView Row Count after Filtering Data in C# ASP.NET

In this post let’s see how we can count the number of Rows in a filtered DataView.

DataView Row Count after Filtering Data in C# ASP.NET

Syntax for DataView Row Count after filter :

DataViewName.Count;

The “Count” will retrieve the number of records in System.Data.DataView after filtering the Data in it. It will return the value as int, DataViewName.Count.ToString() will convert it to String value.

Usage : 

lblCount.Text = “Total record(s) : ” + dv.Count.ToString(); // no. of records, dv is my DataView name or Id

Example :

I’m using NorthWind Database for this project. See this post, how you can install NorthWind Database on SQL Server Database.

My Design page : DataViewCount.aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataViewCount.aspx.cs" Inherits="DataViewCount" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="font-family:Calibri; color:darkblue; padding:10px;">
<h2>ASP.NET DataView Row Count</h2>
<asp:Label runat="server" ID="lblCount" />
<br />
<br />
<asp:GridView runat="server" ID="dataGridView" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" >
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</div>
</div>
</form>
</body>
</html>

My Code page : DataViewCount.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class DataViewCount : System.Web.UI.Page
{
static String connectionString = "Data Source=192.168.0.192;Initial Catalog=Northwind;User ID=sa;Password=789";
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sqlAdapter;

protected void Page_Load(object sender, EventArgs e)
{
FillGrid();
}

public void FillGrid()
{
try
{

con = new SqlConnection(connectionString);
cmd = new SqlCommand("select * from [Quarterly Orders]", con);
sqlAdapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sqlAdapter.Fill(dt);

con.Close();
DataView dv = dt.DefaultView;
dv.RowFilter = "City like 'B%'";
dataGridView.DataSource = dv;

lblCount.Text = "Total record(s) : " + dv.Count.ToString(); // no. of records, dv is my DataView name or Id
dataGridView.DataBind();
}
catch (Exception ex)
{

lblCount.Text = ex.Message.ToString();

}
}
}

Final Result : 

 

 


Exit mobile version