Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have DataSet ds, with 3 tables table0, table1, table3. my requirement is

"sort table0 by given column name and pass this ds to some other method".

i have done this. code is working fine. but i am not happy with doing in this way. :(

C#
string[] sortByVal = sortValue.Split('_');
dsProductsForSearch = HttpContext.Current.Session["dsProductsForSearch"] as DataSet;

DataTable dt0 = dsProductsForSearch.Tables[0];

if (dt0.Columns.Contains(sortByVal[1]))
{
    DataTable dt1 = dsProductsForSearch.Tables[1];
    DataTable dt2 = dsProductsForSearch.Tables[2];

    dsProductsForSearch.Tables.RemoveAt(0);
    dsProductsForSearch.Tables.RemoveAt(0);
    dsProductsForSearch.Tables.RemoveAt(0);

    DataView dv = new DataView(dt0);
    dv.Sort = sortByVal[1] + " ASC";

    dsProductsForSearch.Tables.Add(dv.ToTable());
    dsProductsForSearch.Tables.Add(dt1);
    dsProductsForSearch.Tables.Add(dt2);
}

CreateHTMLforPDF (dsProductsForSearch);



I tried to add table0 but it always added after last DataTable in DataSet. I have to add it at 0th position or i have to sort in-place.

Can any one give suggestion to do it in minimum line of code with high Performance and readability.

thanks in advance. :):):)
Posted
Comments
PIEBALDconsult 19-Aug-15 0:36am    
I don't see why you are copying the DataTable, just set the Sort on the DataTable's DefaultView and be done with it.

http://www.codeproject.com/Tips/850878/On-Why-to-Use-DataViews

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900