Gridview sorting and filtering


In this article I'm trying to explain how to sort and filter gridview data using DataView in ASP.net. using DefaultView of DataView you can able to get the result into DataView object. Now, I tried to explain how to sort and filter data with step by step.

Gridview sorting and filtering:



In this article I'm trying to explain how to sort and filter gridview data using DataView in ASP.net. Now, I tried to explain how to sort and filter data with step by step.

Description:



DataView selectively sorting and filtering datatable data and displayed in controls. First off all DataTable data to be stored in temporary table and using DefaultView of DataView you can able to get the result into DataView object. Once you got that table information then you can able to sort and filter the data based on your requirement.
Sample code to achieve your goal.

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

public partial class ASPnet_GridviewSortFilter : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataRow dr;
DataRow dr1;
DataRow dr2;
DataRow dr3;
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("Emp_Id");
dt.Columns.Add("Ename");
dt.Columns.Add("Sal");

dr = dt.NewRow();
dr["Emp_Id"] = "1";
dr["Ename"] = "Naveen";
dr["Sal"] = "20000";
dt.Rows.Add(dr);

dr1 = dt.NewRow();
dr1["Emp_Id"] = "4";
dr1["Ename"] = "Rajesh";
dr1["Sal"] = "25000";
dt.Rows.Add(dr1);

dr2 = dt.NewRow();
dr2["Emp_Id"] = "3";
dr2["Ename"] = "Karthi";
dr2["Sal"] = "30000";
dt.Rows.Add(dr2);

dr3 = dt.NewRow();
dr3["Emp_Id"] = "2";
dr3["Ename"] = "Pawan";
dr3["Sal"] = "10000";
dt.Rows.Add(dr3);

DataView dv = dt.DefaultView;
dv.Sort = "Emp_Id ASC";
dv.RowFilter = "Sal like '%0000'";

GridView1.DataSource = dv;

GridView1.DataBind();


}
}

Using above code I just try to sort the datatable information based on Emp_Id in ascending order and filter employee details based on his Salary like above.

Output:



Data

Conclusion:



This articel will help you how to perform sorting and filtering DataTabel data while displayed in gridview control.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: