Subscribe Now!

Enter your email address:

Friday, October 22, 2010

Sorting GridView Columns Manually in Asp.net and c#

private const string ASCENDING = " ASC";
 
private const string DESCENDING = " DESC";
 public SortDirection GridViewSortDirection
    {
        
get
        
{
            
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);
        }
        
else
        
{
            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;
        GridView1.DataBind();       
    }


Sorting GridView Columns Manually in Asp.net and c#

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...