.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Gridview sorting

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 5    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Below is sample code to sort GridView in ASP.Net. This example is based in c#

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

public partial class Datagrid : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

}

public void InitControl(DataTable dtData)

{

try

{

ViewState["dtData"] = dtData;

dgcomonGrid.DataSource = dtData;

dgcomonGrid.DataBind();

}

catch (Exception Ex)

{

throw Ex;

}

}

protected void sortmethod(object sender, GridViewSortEventArgs e)

{

try

{

ViewState["sortcolumn"] = e.SortExpression;

if (ViewState["sortorder"] == null)

{

ViewState["sortorder"] = "asc";

}

ViewState["sortorder"] = ViewState["sortorder"].ToString() == "asc" ? "desc" : "asc";

DataView dv = new DataView((DataTable)ViewState["dtData"]);

dv.Sort = ViewState["sortcolumn"] + " " + ViewState["sortorder"];

dgcomonGrid.DataSource = dv;

dgcomonGrid.DataBind();

}

catch (Exception Ex)

{

throw Ex;

}

}

 

}

 



     

Further Readings:

Responses
Author: netgrid         Company URL: http://www.dotnetspark.com
Posted Date: September 20, 2012

A good article, to the point tutorial. Many thanks and great job. It already helped me to solve a problem which i was facing from a lot of days.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-filtering


Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend