IT Community - Software Programming, Web Development and Technical Support

How to sort the datagrid columns in ascending order and decending order using c# dot?

This is a discussion on How to sort the datagrid columns in ascending order and decending order using c# dot? within the C# Programming forums, part of the Software Development category; How to sort the datagrid columns in ascending order and decending order using c# dot net?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-22-2007, 11:11 PM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Question How to sort the datagrid columns in ascending order and decending order using c# dot?

How to sort the datagrid columns in ascending order and decending order using c# dot net?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-22-2007, 11:28 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How to sort the datagrid columns in ascending order and decending order using c#

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.DataGridSortCommandEvent Args 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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break text inside the asp.net datagrid columns that exceeds the width? vadivelanshanmugam HTML, CSS and Javascript Coding Techniques 1 03-05-2008 02:58 AM
Selecting data's by order S.Vinothkumar Database Support 3 09-27-2007 03:12 AM
Row order in cluster & non-cluster index? prasath Database Support 1 08-08-2007 12:35 AM
What are order of precedence and associativity, and how are they used? anbuchezhians Java Programming 1 08-01-2007 11:53 PM
How can you sort the elements of the array in descending order In C#. NET 2005? Archer C# Programming 1 07-24-2007 12:33 AM


All times are GMT -7. The time now is 11:31 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0