You must Sign In to post a response.

ASP.NET2.0 Gridview sorting sample code

Hi Viewers,

I am new to ASP.NET(2.0) i want to write the code for gridview sorting.
Please let me know the sample code.


Comments

Author: Prashant Jadhav11 Nov 2008 Member Level: Silver   Points : -20

<%@ Page language="C#" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




GridView Sorting Example




GridView Sorting Example



forecolor="Red"
runat="server"/>




forecolor="Navy"
runat="server"/>




datasourceid="CustomersSource"
autogeneratecolumns="true"
allowpaging="true"
emptydatatext="No data available."
allowsorting="true"
onsorting="CustomersGridView_Sorting"
onsorted="CustomersGridView_Sorted"
runat="server">







selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>







Author: Yousuf11 Nov 2008 Member Level: Silver   Points : -20

Here is the sample code

<%@ Page language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




GridView Sort Example




GridView Sort Example














Sort by:
runat="server">
CustomerID
CompanyName
Address
City
PostalCode
Country


 

Then by:
runat="server">
CustomerID
CompanyName
Address
City
PostalCode
Country


Sort order:

runat="server">
Ascending
Descending



text="Sort"
onclick="SortButton_Click"
runat="Server"/>








datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="true"
runat="server">






selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>




Author: dotnet programmer02 Feb 2009 Member Level: Gold   Points : 4

protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = gvSorting.DataSource as DataTable;

if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);

gvSorting.DataSource = dvSortedView;
gvSorting.DataBind();
}
}

private string getSortDirectionString(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
if(sortDirection== SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}

return newSortDirection
}