C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Databinding »

C# - Sort the data table using Select Method


Posted Date: 19 Jun 2009    Resource Type: Code Snippets    Category: Databinding
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 15



Suppose we may want to Sort the data table based on a specific column before binding it it the GridView.

There are two ways to do it.

Method 1: Using DataView Sort property

DataTable dt;

DataView dv = new DataView(dt);
dv.Sort = " desc";


We can access the dataview instead of the datatable

Example:


DataTable dt;

DataView dv = new DataView(dt);
dv.Sort = "EmpID Asc";
dt= dv.toTable();

//Bind the data table to DataGridView
DataGridView1.DataSource = dt;
DataGridView1.Refresh();


This will sort the data table in Descending based on Employee ID.


Method 2 : Sorting using Select Method of Data Table


private static void SortDataTable(DataTable dt, string sortCol)
{
//Take a Clone of the DataTable

DataTable newDT = dt.Clone();
int rowCount = dt.Rows.Count; //Rows Count


//Using Select Method to sort the Rows
DataRow[] SortedRows = dt.Select(null, sortCol); // Sort with Column name
for (int i = 0 ; i < rowCount; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=SortedRows[i][j];
}

//Add the Sorted Row
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);
}

//clear the incoming
dt.Rows.Clear();

for(int i = 0; i < newDT.Rows.Count; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=newDT.Rows[i][j];
}

DataRow data_row = dt.NewRow();
data_row.ItemArray = arr;
dt.Rows.Add(data_row);
}

dt.AcceptChanges();

//Bind the Sorted Table to DataGridView

DataGridView1.DataSource = dt;
DataGridView1.Refresh();

}





Related Resources:
Read articles related to Export to Excel

Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
C# - Sort the data table using Select Method  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A Sample GridView Example using ItemTemplate and BulletList
Previous Resource: VB.NET: Comparison of data tables based on Specific Column
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use