Click here to Skip to main content
Licence 
First Posted 18 Sep 2007
Views 11,620
Bookmarked 8 times

Sorting Datagrid Columns

By | 18 Sep 2007 | Article
Code for sorting of data in datagrid according to columns headers

Navneet Sharma's Article for sorting of datagrid columns

Introduction

This article can be used to sort the data according to the columns of a datagrid using windows forms while developing from C# application in .Net 3.0 environments and functions/methods have been defined here. This code has been inherited and implemented by me after a survey on the web and contains the items useful for sorting datagrid in web applications.

Background

This article can be used to sort the data according to the columns of a datagrid using windows forms while developing from C# application in .Net 3.0 environments

Using the code

Copy this code to sort columns of a datagrid on a webform while developing from C# application in .Net 3.0 environments. Write down the event named datagrid1_Sorting() in your code by selecting the event from the event list.

Copy the below code and paste it into your application.

//

protected void dgXMLData_Sorting(object sender, GridViewSortEventArgs e)

{
    try
        {
            SortColumn = e.SortExpression; //Only if your sort expression is like your column name
            SortOrder = Convert.ToString(Session["SortOrder"]);
            DV = (DataView) datagrid1.DataSource;
            DV.Sort = SortColumn + " " + SortOrder;
            datagrid1.DataSource = DV;
            datagrid1.DataBind();
            Session["SortOrder"] = "ASC";
        }
        catch (Exception ex)
        {
            lblErr.Text = "" + ex.Message + "";
        }

}

Language Used: C#, ASP.Net

Platforms: Win XP Professional with SP 2, .Net 3.0

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

This code has been implemented here for the first time in this post and if you want some modifications in it then mail me or suggest me the required improvements and I will do the required changes.

Please vote this article if this was useful to you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Navneet Codes

Web Developer

India India

Member

.Net Developer as well as Trainer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhaving error Pinmembersheebalam21:27 26 Nov '07  
GeneralRe: having error PinmemberNavneet Codes22:22 11 Dec '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 18 Sep 2007
Article Copyright 2007 by Navneet Codes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid