Click here to Skip to main content
14,427,368 members

Two way sorting in DataGrid using Bound Columns

Rate this:
2.74 (17 votes)
Please Sign up or sign in to vote.
2.74 (17 votes)
19 Aug 2004
Two way sorting in DataGrid using bound columns.


In this small code snippet, I will show how to perform two way sorting in DataGrid using bound columns. Remember, there can be numerous ways you can sort the items in the DataGrid and this is just one of them.

Adding Bound Columns:

First, add some columns in your DataGrid using Property Builder. Since this is an intermediate level article, you should know how to perform this. Once you add the bound columns, give the DataGrid a data source and populate it with some data.

Sorting DataGrid both ways:

Okay, up till now, the DataGrid should be populated with some data. Now in the Properties window of the DataGrid, set AllowSorting = "true". Okay, now sorting is enabled and all you need now is the actual code that sorts the DataGrid.

DataGrid Sorting Code:

Whenever the Columns header is clicked, SortCommand event is fired. So, place the code below in the SortCommand event handler:

private void Sort_DataGrid(object source, 
  System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    // Never use Queries like this always use Stored procedures
    SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories", 
    myCommand.CommandType = CommandType.Text;
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    DataView dv = new DataView(ds.Tables["Categories"]);
    if( (numberDiv%2) == 0 )
        dv.Sort = e.SortExpression + " " + "ASC";
        dv.Sort = e.SortExpression + " " + "DESC";
    myDataGrid.DataSource = dv;

In the above code, "numberDiv" is the public static Int32 variable which keeps count of the even and odd number of mouse clicks on the header. The heart of this example is the DataView object which has a SortExpression property, it represents the name of the column clicked.

And lastly, just assign the DataSource to the DataView and bind the grid on the page.


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

Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.

I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

GeneralDatagrid sorting after importing Excel sheet Pin
bhatted18-Apr-11 11:39
Memberbhatted18-Apr-11 11:39 
GeneralAscending and Descending Sorting with DataGrid Columns Pin
John Stagich19-Sep-08 7:46
MemberJohn Stagich19-Sep-08 7:46 
Generalthanks Pin
roxana-chc3-Jan-07 7:47
Memberroxana-chc3-Jan-07 7:47 
Generalstatic member variable Pin
Ashley van Gerven20-Aug-04 22:44
MemberAshley van Gerven20-Aug-04 22:44 
GeneralRe: static member variable Pin
azamsharp21-Aug-04 7:23
Memberazamsharp21-Aug-04 7:23 
GeneralRe: static member variable Pin
Christian Calderon15-Feb-05 5:00
MemberChristian Calderon15-Feb-05 5:00 
GeneralRe: static member variable Pin
L3go1as17-Oct-05 18:00
MemberL3go1as17-Oct-05 18:00 
QuestionRe: static member variable Pin
praveen.aru12-Jan-07 6:54
Memberpraveen.aru12-Jan-07 6:54 

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

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

Posted 19 Aug 2004


30 bookmarked