Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to sort the datatable by LookupService.countryName
any help would be great

C#
[WebMethod(Description = "Get a list of all the countries.")]
[ScriptMethod]
public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category)
{

    List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>();

    DataTable dt = new DataTable();
    dt.Columns.Add("CCode");
    dt.Columns.Add("CName");

    for (int i = 0; i < LookupService.countryCode.Count(); ++i)
    {
        // cCode is code string array
        // cName is name string array
        dt.Rows.Add(LookupService.countryCode[i], LookupService.countryName[i]);
        //l.Add(new CascadingDropDownNameValue(LookupService.countryName[i], LookupService.countryCode[i]));
    }

    return l.ToArray();
}
Posted
Comments
Sandeep Mewara 13-Feb-11 23:50pm    
Looks like you used the answer suggested to you yesterday. Did you close that question? If an answer helps and it resolves your issue, you should upvote and mark the answer accepted. It helps you and others in future.

You can extract sorted data like this:

DataTable dt = new DataTable();

dt.Columns.Add("CCode");
dt.Columns.Add("CName");

dt.Rows.Add("AAA", "ZZZ");
dt.Rows.Add("BBB", "YYY");
dt.Rows.Add("CCC", "XXX");

DataRow[] Rows1 = dt.Select("", "CCode");
DataRow[] Rows2 = dt.Select("", "CName");
 
Share this answer
 
You can sort data table through DataTable.DefaultView

Assuming dt is your data table.

DataView dv = dt.DefaultView;
dv.Sort = "CName ASC";
 
Share this answer
 
v2

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