GridView Binding with DataView Properties.

Posted by Vpramodg under ASP.NET category on | Points: 40 | Views : 10551
Dataview is using for the customized view of data of datatable, with the help of dataview we can filtered,searched or sorted. We can get an instance of a DataView object from the DefaultView property of our DataTable. Then, we can sets its Sort and RowFilter properties:

Here is the code.....

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

public partial class DataViewEx : System.Web.UI.Page
SqlConnection objCon = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter objAda = new SqlDataAdapter();
DataSet objDs = new DataSet();

protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)

objCon.ConnectionString = "Data Source=.;Initial Catalog=Company;Integrated Security=True;";
objCmd.Connection = objCon;
objCmd.CommandText = "select * from emp";
objAda.SelectCommand = objCmd;
objAda.Fill(objDs, "emp");

DataView dataView = objDs.Tables["emp"].DefaultView;
dataView.RowFilter = "name like 'p%'";
dataView.Sort = "marks DESC";
GridView1.DataSource = dataView;



Comments or Responses

Login to post response