C# .NET - what is DataView?what is use of it? - Asked By swati deshpande on 24-Feb-06 05:37 AM


A DataView is a collection - Asked By Peter Bromberg on 24-Feb-06 06:39 AM

of DataRowView objects, each representing a row in a DataTable. DataRowViews are used for sorting and filtering. Every DataTable has an automatic DataRowView property, "DefaultView".

DataView - Asked By Shallu Gupta on 24-Feb-06 06:44 AM

You can releate DataView as Views in Database where we can restrict the columns, can specify the filter criteria and sorting criteria.
This article will help you clear your doubts ablout DataView.


here are some imp points - Asked By Venkat K on 24-Feb-06 07:16 AM

if you want to see some advantages with dataview see this link

There are two ways to create a DataView. You can use the DataView constructor, or you can create a reference to the DefaultView property of the DataTable. 
The following code example demonstrates how to create a DataView using the DataView constructor. A RowFilter, Sort column, and DataViewRowState are supplied along with the DataTable.

DataView custDV = new DataView(custDS.Tables["Customers"], 
                               "Country = 'USA'", 
The following code example demonstrates how to obtain a reference to the default DataView of a DataTable using the DefaultView property of the table.
DataView custDV = custDS.Tables["Customers"].DefaultView;
Check this - Asked By mahesh pesani on 24-Feb-06 07:19 AM

By using the dataview u can do the sorting, finding and filtering of the data.

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.

A DataView provides a dynamic view of data whose content, ordering, and membership reflect changes to the underlying DataTable as they occur. This is different from the Select method of the DataTable, which returns a DataRow array from a table per a particular filter and/or sort order and whose content reflects changes to the underlying table, but whose membership and ordering remain static. The dynamic capabilities of the DataView make it ideal for data-binding applications.

A DataView provides you with a dynamic view of a single set of data to which you can apply different sorting and filtering criteria, similar to the view provided by a database. However, a DataView differs significantly from a database view in that the DataView cannot be treated as a table and cannot provide a view of joined tables. You also cannot exclude columns that exist in the source table, nor can you append columns, such as computational columns, that do not exist in the source table.


about Dataview with example in C# coding - krish raju replied to swati deshpande on 05-Jun-08 01:02 AM

while we are showing the record in a DataGrid.)

    When we show our record in a DataGrid, then if we want to show our record with a specific condition or in a specific order then here we have to use DataView and its property. Let see with this programme how we can use DataView and its properties.

Here in aspx code, I used a DataGrid.

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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Data View</title>



    <form id="form1" runat="server">


     <table cellpadding ="0" cellspacing="0">


           <asp:DataGrid ID="DataGridShowRecord" runat="server"></asp:DataGrid>







The cs code file is:


using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page


    SqlDataAdapter da;

    SqlConnection con;

    DataSet ds = new DataSet();

    DataView dv;

    SqlCommand cmd = new SqlCommand();

    protected void Page_Load(object sender, EventArgs e)


        con = new SqlConnection("Data Source=MCN0100;Initial Catalog=Employee; Uid=sa; pwd=");

        cmd.CommandText = "select * from EmployeeRecord";

        cmd.Connection = con;

        da=new SqlDataAdapter(cmd);





        //Here Storing the table in DataView

        dv = new DataView(ds.Tables[0]);


        //Here Sort Property we can show the record According to Dept No Either Ascending or Descnding order

        dv.Sort = ("DeptNo DESC");


        //From here by using we can filter the Record with with any condition

        dv.RowFilter = ("Location='Noida'");

        DataGridShowRecord.DataSource = dv;





When user will run the application then the record will come in DataGrid with descending order of DeptNo and where location is Noida being of DataVew's sort and filter property.

Figure 1.The Records are coming after using sort and filter property of DataView
krishna replied to krish raju on 19-May-10 03:11 AM
HI, krish...

 Your article is simply superb....i was searching to find about dataview for many hours...atlost i got the solution....thanx alot.

ARTICLE - KAMAL replied to swati deshpande on 03-Sep-10 02:53 AM
Ravi Revelly replied to krish raju on 06-Sep-10 03:20 AM
Nice Post!, Thanks, are there any other useful advantages?

priyanka replied to krish raju on 21-Sep-10 02:02 AM
i m using datatable for runtime binding and then use code

DataView dv = dtage.DefaultView;

dv.Sort = "Age";

gridagereport.DataSource = dv;


Session["ds"] = dtage;
but it is not working.