开发者

Sort and bind datarow to dropdownlist

开发者 https://www.devze.com 2023-03-06 07:16 出处:网络
string _Code =开发者_C百科 \"21\"; IEnumerable<DataRow> drs = GetCodes(_Code); foreach (DataRow items in drs)

string _Code =开发者_C百科 "21";

        IEnumerable<DataRow> drs = GetCodes(_Code);
        foreach (DataRow items in drs)
        {
            ListItem li = new ListItem(items["CallingCode"].ToString(), items["CountryID"].ToString());
            ddCountry.Items.Add(li);
        }

//How can i sort the dropdownlist by calling code.


This might help...

Sort IEnumerable and List with property name


You could use LINQ to sort drs before creating the list of items...

var orderedDRS = drs.OrderBy(row => row["CallingCode"].ToString());
foreach (DataRow items in orderedDRS)
    {
        ...
    }

Where row => row["CallingCode"].ToString() is a lambda expression that selects the row's "CallingCode" as the sort key for OrderBy

You could use LINQ further to bind the DropDown without a foreach loop by Selecting out the CallingCode and CountryID values

0

精彩评论

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