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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding ="0" cellspacing="0">
<tr><td>
<asp:DataGrid ID="DataGridShowRecord" runat="server"></asp:DataGrid>
</td></tr>
</table>
</div>
</form>
</body>
</html>
The cs code file is:
using System;
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);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
//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;
DataGridShowRecord.DataBind();
}
}
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.