开发者

Sort and bind datarow to dropdownlist

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜