|
| 1 | +--- |
| 2 | +title: Sorting RadDropDownList Numerically by Value |
| 3 | +description: Learn how to sort items in a RadDropDownList numerically by their values using code-behind logic in ASP.NET AJAX. |
| 4 | +type: how-to |
| 5 | +page_title: Sorting RadDropDownList Items by Numeric Value in ASP.NET AJAX |
| 6 | +slug: dropdownlist-sorting-numerically-by-value |
| 7 | +tags: asp.net ajax, dropdownlist, sorting, numeric, code-behind |
| 8 | +res_type: kb |
| 9 | +ticketid: 1686740 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadDropDownList for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +I want to sort the items of a [RadDropDownList](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/overview) numerically based on their `Value` property instead of the `Text` property. By default, the control sorts items by `Text`. Is there a way to sort the RadDropDownList by numeric values programmatically? |
| 30 | + |
| 31 | +This knowledge base article also answers the following questions: |
| 32 | + |
| 33 | +- How can I sort RadDropDownList items by value in ASP.NET AJAX? |
| 34 | +- Is it possible to perform numeric sorting on RadDropDownList? |
| 35 | +- How to sort RadDropDownList using code-behind? |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +To sort the RadDropDownList items numerically by their `Value`, you can perform custom sorting in the code-behind. The following example demonstrates how to achieve this: |
| 40 | + |
| 41 | +### Code Example |
| 42 | + |
| 43 | +```` |
| 44 | +protected void Page_Load(object sender, EventArgs e) |
| 45 | +{ |
| 46 | + if (!IsPostBack) |
| 47 | + { |
| 48 | + // Define RadDropDownList items |
| 49 | + List<DropDownListItem> tourItems = new List<DropDownListItem> |
| 50 | + { |
| 51 | + new DropDownListItem("Alps Tour", "100"), |
| 52 | + new DropDownListItem("Desert Safari", "25"), |
| 53 | + new DropDownListItem("Island Escape", "7"), |
| 54 | + new DropDownListItem("Mountain Hike", "50"), |
| 55 | + new DropDownListItem("City Lights", "300") |
| 56 | + }; |
| 57 | +
|
| 58 | + // Sort by Value numerically |
| 59 | + List<DropDownListItem> sortedItems = tourItems.OrderBy(item => int.Parse(item.Value)).ToList(); |
| 60 | +
|
| 61 | + // Clear existing items and add sorted items |
| 62 | + RadDropDownList1.Items.Clear(); |
| 63 | + foreach (DropDownListItem item in sortedItems) |
| 64 | + { |
| 65 | + RadDropDownList1.Items.Add(item); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +```` |
| 70 | + |
| 71 | +Explanation |
| 72 | + |
| 73 | +1. **Sorting Items**: The code uses LINQ to sort the items based on their numeric `Value`. The `int.Parse` method is used to ensure the sorting is numerical rather than alphabetical. |
| 74 | +2. **Re-adding Items**: After sorting, the existing items are cleared using `RadDropDownList1.Items.Clear()`. The sorted items are then added back to the control. |
| 75 | + |
| 76 | +Notes |
| 77 | + |
| 78 | +- Ensure that all `Value` properties can be parsed into integers. If the values are non-integer, adjust the parsing logic accordingly. |
| 79 | +- This approach is ideal when you need to customize sorting behavior for RadDropDownList items. |
| 80 | + |
| 81 | +## See Also |
| 82 | + |
| 83 | +- [RadDropDownList Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/overview) |
| 84 | +- [RadComboBox vs RadDropDownList](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/dropdownlist-vs-radcombobox) |
| 85 | +- [Sorting Items in RadComboBox](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview) |
0 commit comments