Sorting and Paging Gridview without datasource control
Posted on 4/14/2008 9:42:55 AM
in #ASP.NET 2.X
Hi
/P>
Many a times while working with Gridview we want to work with the paging and sorting functionality without using any datasource control. Gridview is flexible enough to perform these tasks without the use of any datasource control and only a few lines of code. /SPAN>/P>
For paging a Gridview manually we have to handle the Gridview’s PageIndexChanged Event. /SPAN>In the event we need to change the PageIndex of Gridview to the page that user has clicked and again bind the Gridview./P>
protected void gvPaging_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvPaging.PageIndex = e.NewPageIndex; gvPaging.DataBind(); }
This is all you need to do to make manual paging in the Gridview./P>
For manually sorting in Gridview we need to handle the Sorting event of Gridview. Here is the code to do it./P>
protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e) { DataTable dtSortTable = gvSorting.DataSource as DataTable;/P>
if (dtSortTable != null) { DataView dvSortedView = new DataView(dtSortTable); dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);/P>
gvSorting.DataSource = dvSortedView; gvSorting.DataBind(); } }/P>
private string getSortDirectionString(SortDirection sortDireciton) { string newSortDirection = String.Empty; if(sortDirection== SortDirection.Ascending) { /SPAN>newSortDirection = "ASC"; } else { /SPAN>newSortDirection = "DESC"; }/P>
return newSortDirection }/P>And we have the manual sorting for the Gridview.
Vikram/SPAN>
|
Posted on 3/19/2008 12:32:35 PM
I can definitely see some uses for this. I'll give it a try but let me ask you this. When it writes out the value or the dictionary entry is it just writing the value of ToString()?
/p>
|
Posted on 7/15/2009 6:54:33 AM
Thanks it was very helpful for me
/p>
|
Posted on 7/13/2010 8:28:15 AM
Don't we need the ViewState method??
/p>
|
Posted on 11/9/2010 8:59:49 PM
I am getting null in
DataTable dtSortTable = gvSorting.DataSource as DataTable;
/p>
But I can abviously see that my gvSorting.DataSource in not null.
/p>
Can you help me pls pls pls!
/p>
|