Paging and Sorting of Grid View


This article gives a small example of using Gridview implementing paging and sorting using Asp.Net 2.0 . it is helpful for new learner

Gridview Paging and Sorting



Gridview control Displays the values of a data source in a table where each column represents a field and each row represents a record.

Check the sample code below



AllowPaging:

Gets or sets a value indicating whether the paging feature is enabled.

AllowSorting:

Gets or sets a value indicating whether the sorting feature is enabled.

In aspx code please write the gridview as


<asp:GridView
AllowPaging=true
AllowSorting=true
ID="grdView"
runat="server"
AutoGenerateColumns=false
Width="50%"
GridLines="Horizontal"
PageSize =5
OnPageIndexChanging="grdView_PageIndexChanging"
OnSorting ="grdView_OnSorting"
DataKeyNames="CustomerName" HeaderStyle-ForeColor=green HeaderStyle-BackColor=RosyBrown
>

<RowStyle Backcolor="#e1e1ff" />
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="CustomerName" HeaderStyle-ForeColor=Green>
<ItemTemplate>
<asp:Label ID="lblFirstName" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City" HeaderStyle-Font-Underline=true HeaderStyle-ForeColor=white>
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# DataBinder.Eval(Container.DataItem, "City") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




In Code Behind using the following code......




public partial class GridViewPgSort : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataView dv = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dv = bindgrid();
grdView.DataSource = dv;
grdView.DataBind();
}
}

private DataView bindgrid()
{
String ConnString = "Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True";
String StrQuery = "select * from TableName";
SqlDataAdapter sadp = new SqlDataAdapter(StrQuery, ConnString);
sadp.Fill(ds);
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
return dv;
}

//Sorting Event:




protected void grdView_OnSorting (Object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
grdView.DataSource = bindgrid();
grdView.DataBind();
}

<H2>// Paging Event
</H2>

protected void grdView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
grdView.PageIndex = e.NewPageIndex;
grdView.DataSource = bindgrid();
grdView.DataBind();
}

}


Comments

Author: DotNetGal [ss.mmm]04 Jan 2008 Member Level: Silver   Points : 0

Paging is working fine. Sorting is not working for me.

Author: Suresh27 Mar 2008 Member Level: Silver   Points : 0

It's good and simple to use.Wht i am looking is I got here.Thx alot .

Author: Suresh25 Apr 2008 Member Level: Silver   Points : 0

Thanks for the sample code...
It was simple, clear and really helpful...

Author: Ramya28 Jul 2008 Member Level: Bronze   Points : 1

Its really very helpful.

Thanks for Submitting this article.

It would be helpful,if u submit for Filtering and sorting..

Regards,
Ramya

Author: Zelalem08 Oct 2008 Member Level: Gold   Points : 2

Hi,

It is really very helpful and for those who said either sorting or paging doesn't work, please make sure:OnPageIndexChanging="grdView_PageIndexChanging" OnSorting ="grdView_OnSorting" are included in your:
tag in your hlml file.
Or probably make sure the gridview's ID is grdView.

The article is so useful!
Zelalem

Author: lovelydpka09 Mar 2010 Member Level: Bronze   Points : 1

Thanks, Paging is working fine but Sorting is not. I think bcoz of parameter "sortExpr" which needs to bind with some data. Can anyone tell me what is "ViewState["sortExpr"]" ... specially what is "sortExpr" and where are we defining this.

Author: Aneela09 Apr 2010 Member Level: Bronze   Points : 2

Hi lovelydpka,

I have found great article on manual asp:gridview paging and sorting.
Follow the links below to get complete insight of these Phenomenon.

For GridView Sorting
For GridView Paging


Gud Luck

Author: Imtiaz Ahmed27 Apr 2010 Member Level: Silver   Points : 2

Hi lovelydpka,

The only thing missing in the above code is that below
dv.Sort = (string)ViewState["sortExpr"] + " " + sortingOrder;

just add the above line to the existing code then the code works fine..

we need to sort the dataview by sort direction as well...

One more thing need to include the below fucntion which gets the sort direction value.

public string sortingOrder
{
get
{
if(ViewState["sortingOrder"].ToString() == "desc")
ViewState["sortingOrder"] = "asc";
else
ViewState["sortingOrder"] = "desc";

return ViewState["sortingOrder"].ToString();
}
set
{
ViewState["sortingOrder"] = value;
}
}


Hope this helps...

Bye

Author: kuldeep16 Sep 2010 Member Level: Gold   Points : 0

thanks for sharing
regard
Kuldeep

Author: Santha Kumar M27 Jan 2011 Member Level: Bronze   Points : 1

I founded one website for gridview header row filtering and sorting sample. i hope helpful us.
www.aspdotnetexample.com

Thanks
santhakumar



  • 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: