Click here to Skip to main content
6,594,932 members and growing! (16,271 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Two way sorting in DataGrid using Bound Columns

By azamsharp

Two way sorting in DataGrid using bound columns.
C#, Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:19 Aug 2004
Views:76,085
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.37 Rating: 2.74 out of 5
4 votes, 23.5%
1
4 votes, 23.5%
2
1 vote, 5.9%
3
2 votes, 11.8%
4
6 votes, 35.3%
5

Introduction

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", 
                                                         myConnection);
    myCommand.CommandType = CommandType.Text;
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    myAdapter.Fill(ds,"Categories");
    DataView dv = new DataView(ds.Tables["Categories"]);
    if( (numberDiv%2) == 0 )
        dv.Sort = e.SortExpression + " " + "ASC";
    else
        dv.Sort = e.SortExpression + " " + "DESC";
    numberDiv++;
    myDataGrid.DataSource = dv;
    myDataGrid.DataBind();
}

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.

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

azamsharp


Member
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

HighOnCoding.com www.HighOnCoding.com


My Blog:

Blog

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralAscending and Descending Sorting with DataGrid Columns PinmemberJohn Stagich7:46 19 Sep '08  
Generalthanks Pinmember7:47 3 Jan '07  
Generalstatic member variable PinmemberAshley van Gerven22:44 20 Aug '04  
GeneralRe: static member variable Pinmemberazamsharp7:23 21 Aug '04  
GeneralRe: static member variable PinmemberChristian Calderon5:00 15 Feb '05  
GeneralRe: static member variable PinmemberL3go1as18:00 17 Oct '05  
QuestionRe: static member variable Pinmemberpraveen.aru6:54 12 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Aug 2004
Editor: Smitha Vijayan
Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project