Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

  • Advertisements

Sorting DataGrid

Posted by Praveen Kumar on February 15, 2008

 We all know that GridView columns can be sorted automatically when SQLDataSource is being used. But what if you are not using SqlDataSource to populate the GridView. Sorting the GridView manually is pretty straight forward task take a look at the code below. private const string ASCENDING = ” ASC”;
private const string DESCENDING = ” DESC”;

 public SortDirection GridViewSortDirection
if (ViewState[“sortDirection”] == null)
                ViewState[“sortDirection”] = SortDirection.Ascending;

            return (SortDirection) ViewState[“sortDirection”];                
set { ViewState[“sortDirection”] = value; } 

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING); 

    private void SortGridView(string sortExpression,string direction)
//  You can cache the DataTable for improving performance
DataTable dt = GetData().Tables[0]; 

        DataView dv = new DataView(dt); 
        dv.Sort = sortExpression + direction;         

        GridView1.DataSource = dv;


2 Responses to “Sorting DataGrid”

  1. aaditya said

    it is possible to create a dynamic gridview in windows form using C#
    . do you have the code

  2. Yes,
    look at Bind Data to the Windows Forms DataGridView Control


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: