开发者

C# Sorting a DataTableCollection by a common column in each table

开发者 https://www.devze.com 2023-03-10 00:50 出处:网络
I\'m having some troubles figuring out how to sort a DataTableCollecti开发者_C百科on.The scenario is that each table in the collection would have the same schema and have a column called \"JobNumber\"

I'm having some troubles figuring out how to sort a DataTableCollecti开发者_C百科on. The scenario is that each table in the collection would have the same schema and have a column called "JobNumber" which I want to sort on. The data in these tables would need to be processed in that order.

Any suggestions?


DataTableCollection col;

  foreach(DataTable tbl in col)
  {
    // Get the DefaultViewManager of a DataTable and sort it.
    DataTable1.DefaultView.Sort = "JobNumber";
  }

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx


Well it's easy to iterate throught the tables in the collection, but as for sorting each one, you may want to see: http://msdn.microsoft.com/en-us/library/zk13kdh0(v=vs.71).aspx


I would use SortedList fill it with rows from all DataTables, that's if your JobNumer is unique, something like that :

public SortedList<int, DataRow> SortDataTableCollection(DataTableCollection col)
{
  SortedList<int, DataRow> result = new SortedList<int, DataRow>();
  foreach(DataTable tbl in col)
  {
    foreach(DataRow rw in tbl.Rows)
    {
      result.Add((int)rw["JobNumber"], rw);
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消