IT Community - Software Programming, Web Development and Technical Support

How to sort Datatable

This is a discussion on How to sort Datatable within the C# Programming forums, part of the Software Development category; Hi, I have a datatable that contains the values in no order. I want to arrange it by name. Can ...


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 03-07-2008, 09:05 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default How to sort Datatable

Hi,

I have a datatable that contains the values in no order. I want to arrange it by name. Can anyone give me an idea to do this..
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008, 09:06 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 181
Balasubramanian.S is on a distinguished road
Default Re: How to sort Datatable

Hi,

Hope this helps..

private DataTable SortDataTable(DataTable dt, string sort)
{
DataTable newDT = dt.Clone();
int rowCount = dt.Rows.Count;

DataRow[] foundRows = dt.Select(null, sort); // 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] = foundRows[i][j];
}
DataRow data_row = newDT.NewRow();
data_row.ItemArray = arr;
newDT.Rows.Add(data_row);
}

//clear the incoming dt
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);
}
return dt;
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-22-2008, 09:25 PM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How to sort Datatable

public void Page_Load(Object sender, EventArgs e) {

ArrayList codes = new ArrayList();
codes.Add("20040307");
codes.Add("20051103");
codes.Add("20030503");
codes.Add("20041225");
Show(codes);

//ascending order
Response.Write("<hr/>");
ArrayList sortedCodesAcending = SortArrayList(codes);
Show(sortedCodesAcending);

//descending order
Response.Write("<hr/>");
ArrayList sortedCodesDescending = SortArrayListReverseOrder(codes);
Show(sortedCodesDescending);

}

private ArrayList SortArrayList(ArrayList alOld) {

SortedList sl = new SortedList();
ArrayList alNew = new ArrayList();

//copy values from arraylist into sortedlist
foreach(string s in alOld) {
sl.Add(s,"dummy");
}

//copy values from sortedlist back into arraylist
foreach(string s in sl.Keys) {
alNew.Add(s);
}

return alNew;

}

private ArrayList SortArrayListReverseOrder(ArrayList alUnsorted) {

SortedList sl = new SortedList();
ArrayList alAscending = new ArrayList();
ArrayList alDescending = new ArrayList();

//copy values from arraylist into sortedlist
foreach(string s in alUnsorted) {
sl.Add(s,"dummy");
}

//copy values from sortedlist back into arraylist
foreach(string s in sl.Keys) {
alAscending.Add(s);
}

//reverse the order (comment this out if you want ascending order)
for(int x=alAscending.Count-1; x >=0; x--) {
alDescending.Add(alAscending[x]);
}

return alDescending;

}

private void Show(ArrayList al) {
foreach(string s in al) {
HttpContext.Current.Response.Write(s.ToString() + "<br/>");
}
}
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 get distinct records from datatable oxygen C# Programming 1 03-05-2008 01:47 AM
How will get distinct values from datatable Mramesh C# Programming 5 02-14-2008 07:51 PM
Convert Dataview to Datatable it.wily C# Programming 2 01-23-2008 01:51 AM
Cannot implicitly convert type 'void' to 'System.Data.DataTable' oxygen C# Programming 7 07-30-2007 09:08 AM
How to create Datatable which is very useful in binding.. Archer C# Programming 1 07-21-2007 12:12 AM


All times are GMT -7. The time now is 04:39 AM.


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

SEO by vBSEO 3.0.0