Introduction
Today we are working with GridView custom sorting and paging without using any datasource control in Asp.net. Gridview is flexible enough to perform these tasks without the use of any datasource control and only a few lines of code. For paging a Gridview manually we have to handle the Gridview’s PageIndexChanged Event. In the event we need to change the PageIndex of Gridview to the page that user has clicked and again bind the Gridview.
Gridview Paging.
Today we are working with GridView custom sorting and paging without using any datasource control in Asp.net. Gridview is flexible enough to perform these tasks without the use of any datasource control and only a few lines of code. For paging a Gridview manually we have to handle the Gridview’s PageIndexChanged Event. In the event we need to change the PageIndex of Gridview to the page that user has clicked and again bind the Gridview.
Gridview Paging.
protected
void
GridView1_PageIndexChanging(object
sender,
GridViewPageEventArgs
e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.AllowPaging =
true;
GridView1.PageSize = 3;
GridView1.DataBind();
}
This is all you need to do to make
manual paging in the Gridview.
Gridview Sorting
protected
void
GridView1_Sorting(object
sender,
GridViewSortEventArgs
e)
{
DataTable
_dataTable = GridView1.DataSource
as
DataTable;
if
(_dataTable !=
null)
{
DataView
_dataView =
new
DataView(_dataTable);
_dataView.Sort=e.SortExpression +
""
+ getDirection(e.SortDirection);
GridView1.DataSource=_dataView;
GridView1.DataBind();
}
}
private
string
getDirection(SortDirection
_sortDirection)
{
string
_newDirection =
string.Empty;
if
(_sortDirection ==
SortDirection.Ascending)
{
_newDirection =
"ASC";
}
else
{
_newDirection =
"DESC";
}
return
_newDirection;
}
And we have the manual sorting for the
Gridview.