DataTable sorting

This is a discussion on DataTable sorting within the C# Programming forums, part of the General Programming Boards category; Hi guys, This seems like a simple problem but I'm having difficulty finding the most efficient method. I have a ...

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    DataTable sorting

    Hi guys,

    This seems like a simple problem but I'm having difficulty finding the most efficient method. I have a DataTable with a list of comments (each row represents a comment). There can be replies to comments, so obviously I have to keep track of parent and child comments some how. So what I have is an id column and a parent_id column. the parent_id column points to the parent row if the comment is a reply obviously (0 if not).

    I want to reorder my DataTable so that the comments are in order so I can display them on my page. I can change the DB structure if need and you think theres a better way butt hat my setup right now and I'm kinda stuck as to how to efficiently reorder my DataTable without having to call the DB again.

    if you need any clarification. let me know.

    Thanks in advance,
    Boomba

  2. #2
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    From http://www.csharpfriends.com/Forums/...x?PostID=36859

    Code:
    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); 
    } 
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Exporting a DataTable to Excel (.NET)
    By Trauts in forum C# Programming
    Replies: 0
    Last Post: 07-29-2005, 01:03 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21