Skip to content

Commit 9f10e09

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article grid-sort-columns-by-month-number-based-on-month-name (#621)
Co-authored-by: KB Bot <[email protected]>
1 parent d5c0dbd commit 9f10e09

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Sort Columns by Month Number Based on Month Name
3+
description: This article explains how to sort a RadGrid column by month number when the month name is clicked, ensuring correct chronological order.
4+
type: how-to
5+
page_title: Sort Columns by Month Number Based on Month Name
6+
slug: grid-sort-columns-by-month-number-based-on-month-name
7+
tags: radgrid, asp.net ajax, sorting, gridboundcolumn, sortexpression
8+
res_type: kb
9+
ticketid: 1668677
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>RadGrid 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+
When sorting a grid column by month name, the months are ordered alphabetically, resulting in an incorrect chronological sequence (e.g., December appears before January). I want to sort the column based on the month number when the month name is clicked while maintaining the sort indicator on the month name column.
30+
31+
This KB article also answers the following questions:
32+
33+
- How to sort RadGrid columns chronologically by month name?
34+
- How to use a hidden column for sorting in RadGrid?
35+
- How to maintain the sort indicator on a RadGrid column while sorting by a different field?
36+
37+
## Solution
38+
39+
To sort a [RadGrid](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/overview) column by month number while displaying and sorting by the month name, follow these steps:
40+
41+
1. Add a hidden column to the RadGrid that holds the month number.
42+
2. Set the `SortExpression` of the month name column to the field name of the month number.
43+
3. Ensure the data source includes both month name and month number.
44+
45+
Here's an example implementation:
46+
47+
````ASP.NET
48+
<telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="False">
49+
<MasterTableView>
50+
<Columns>
51+
<telerik:GridBoundColumn DataField="MonthName" HeaderText="Month" SortExpression="MonthNumber" UniqueName="MonthName" />
52+
<telerik:GridBoundColumn DataField="MonthNumber" HeaderText="Month #" UniqueName="MonthNumber" Visible="false" />
53+
</Columns>
54+
</MasterTableView>
55+
</telerik:RadGrid>
56+
````
57+
58+
And the corresponding backend code in VB.NET:
59+
60+
````C#
61+
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
62+
{
63+
RadGrid1.DataSource = GetData();
64+
}
65+
66+
private DataTable GetData()
67+
{
68+
DataTable dt = new DataTable();
69+
dt.Columns.Add("MonthName", typeof(string));
70+
dt.Columns.Add("MonthNumber", typeof(int));
71+
72+
dt.Rows.Add("February", 2);
73+
dt.Rows.Add("April", 4);
74+
dt.Rows.Add("March", 3);
75+
dt.Rows.Add("May", 5);
76+
dt.Rows.Add("June", 6);
77+
dt.Rows.Add("January", 1);
78+
dt.Rows.Add("July", 7);
79+
dt.Rows.Add("September", 9);
80+
dt.Rows.Add("November", 11);
81+
dt.Rows.Add("October", 10);
82+
dt.Rows.Add("December", 12);
83+
dt.Rows.Add("August", 8);
84+
85+
return dt;
86+
}
87+
````
88+
````VB
89+
Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs)
90+
RadGrid1.DataSource = GetData()
91+
End Sub
92+
93+
Private Function GetData() As DataTable
94+
Dim dt As DataTable = New DataTable()
95+
dt.Columns.Add("MonthName", GetType(String))
96+
dt.Columns.Add("MonthNumber", GetType(Integer))
97+
dt.Rows.Add("February", 2)
98+
dt.Rows.Add("April", 4)
99+
dt.Rows.Add("March", 3)
100+
dt.Rows.Add("May", 5)
101+
dt.Rows.Add("June", 6)
102+
dt.Rows.Add("January", 1)
103+
dt.Rows.Add("July", 7)
104+
dt.Rows.Add("September", 9)
105+
dt.Rows.Add("November", 11)
106+
dt.Rows.Add("October", 10)
107+
dt.Rows.Add("December", 12)
108+
dt.Rows.Add("August", 8)
109+
Return dt
110+
End Function
111+
````
112+
113+
This setup ensures that clicking the month name column sorts the data based on the numerical month value while keeping the sort indicator on the name column.
114+
115+
## See Also
116+
117+
- [Basic Sorting](https://demos.telerik.com/aspnet-ajax/grid/examples/functionality/sorting/basic-sorting/defaultcs.aspx)
118+
- [Sorting in RadGrid](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/functionality/sorting/overview)

0 commit comments

Comments
 (0)