
tmlipinski
Top achievements
Rank 1
tmlipinski
asked on 20 May 2009, 04:29 PM
Hi,
Let's assume there is a grid displaying application users. There are various columns and among them: name, surname and login. I would like this grid to behave this way:
- when I click the "name" column header it sorts by: name, surname
- when I click the "surname" column header it sorts by: surname, name
- when I click the "login" column header it sorts by: login
In the MS DataGrid it can be easily achieved setting SortExpression values. accordingly:
- "name, surname"
- "surname, name"
- "login"
Is it possible to do the same using RadGrid? The SortExpression property must contain a single field name. If AllowMultiColumnSorting = true, clicking columns' headers just adds or subtracts columns from the current set of sorting expressions. The above is possible but requires too much clicking and unneccessary intermediate sortings (e.g.: the current sorting is "name, surname"; I want to sort by "login"; I must click "name", then "surname" and "login" at last; three click and sortings instead of one).
Can you help me?
Regards
Tomasz
PS. Multi value SortExpression properties ("name, surname") worked in the "classic" RadGrid...
Let's assume there is a grid displaying application users. There are various columns and among them: name, surname and login. I would like this grid to behave this way:
- when I click the "name" column header it sorts by: name, surname
- when I click the "surname" column header it sorts by: surname, name
- when I click the "login" column header it sorts by: login
In the MS DataGrid it can be easily achieved setting SortExpression values. accordingly:
- "name, surname"
- "surname, name"
- "login"
Is it possible to do the same using RadGrid? The SortExpression property must contain a single field name. If AllowMultiColumnSorting = true, clicking columns' headers just adds or subtracts columns from the current set of sorting expressions. The above is possible but requires too much clicking and unneccessary intermediate sortings (e.g.: the current sorting is "name, surname"; I want to sort by "login"; I must click "name", then "surname" and "login" at last; three click and sortings instead of one).
Can you help me?
Regards
Tomasz
PS. Multi value SortExpression properties ("name, surname") worked in the "classic" RadGrid...
5 Answers, 1 is accepted
0

Nate Pinchot
Top achievements
Rank 1
answered on 20 May 2009, 08:10 PM
Tomasz,
I would have to agree with you that the SortExpression property seems to have a bug and it should allow multiple columns when AllowMultiColumnSorting is true.
I was however able to accomplish what you want to do.
I hope this helps you out.
Default.aspx:
Default.aspx.cs:
I would have to agree with you that the SortExpression property seems to have a bug and it should allow multiple columns when AllowMultiColumnSorting is true.
I was however able to accomplish what you want to do.
I hope this helps you out.
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title></title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<div> |
<asp:ScriptManager ID="ScriptManager1" runat="server"> |
</asp:ScriptManager> |
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="True" |
GridLines="None" Skin="Vista" OnSortCommand="RadGrid1_SortCommand"> |
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ContactID" DataSourceID="SqlDataSource1" |
AllowMultiColumnSorting="true"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="ContactID" DataType="System.Int32" HeaderText="ContactID" |
ReadOnly="True" SortExpression="ContactID" UniqueName="ContactID"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Title" HeaderText="Title" SortExpression="Title" |
UniqueName="Title"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="FirstName" HeaderText="Name" SortExpression="FirstName" |
UniqueName="FirstName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="MiddleName" HeaderText="MiddleName" SortExpression="MiddleName" |
UniqueName="MiddleName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="LastName" HeaderText="Surname" SortExpression="LastName" |
UniqueName="LastName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Suffix" HeaderText="Suffix" SortExpression="Suffix" |
UniqueName="Suffix"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="EmailAddress" HeaderText="EmailAddress" SortExpression="EmailAddress" |
UniqueName="EmailAddress"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Phone" HeaderText="Phone" SortExpression="Phone" |
UniqueName="Phone"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="ModifiedDate" DataType="System.DateTime" HeaderText="ModifiedDate" |
SortExpression="ModifiedDate" UniqueName="ModifiedDate"> |
</telerik:GridBoundColumn> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" |
SelectCommand="SELECT TOP 500 [ContactID] |
,[NameStyle] |
,[Title] |
,[FirstName] |
,[MiddleName] |
,[LastName] |
,[Suffix] |
,[EmailAddress] |
,[EmailPromotion] |
,[Phone] |
,[PasswordHash] |
,[PasswordSalt] |
,[rowguid] |
,[ModifiedDate] |
FROM [AdventureWorks].[Person].[Contact]"></asp:SqlDataSource> |
</div> |
</form> |
</body> |
</html> |
Default.aspx.cs:
using System; |
using System.Web.UI.WebControls; |
using Telerik.Web.UI; |
namespace WebApplication1 |
{ |
public partial class _Default : System.Web.UI.Page |
{ |
protected void Page_Load(object sender, EventArgs e) |
{ |
} |
private string GetSqlSortOrder(GridSortOrder gridSortOrder) |
{ |
switch (gridSortOrder) |
{ |
case GridSortOrder.Ascending: |
return "ASC"; |
case GridSortOrder.Descending: |
return "DESC"; |
default: |
return ""; |
} |
} |
protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e) |
{ |
if (e.CommandSource is LinkButton) |
{ |
LinkButton linkButton = (LinkButton) e.CommandSource; |
if (linkButton.Text == "Name" || linkButton.Text == "Surname") |
{ |
RadGrid1.MasterTableView.SortExpressions.Clear(); |
GridSortOrder newSortOrder = GridSortOrder.Ascending; |
// reverse previous sort order |
if (ViewState["PreviousSortOrder"] != null) |
{ |
GridSortOrder gridSortOrder = (GridSortOrder) ViewState["PreviousSortOrder"]; |
switch (gridSortOrder) |
{ |
case GridSortOrder.Ascending: |
newSortOrder = GridSortOrder.Descending; |
break; |
default: |
newSortOrder = GridSortOrder.Ascending; |
break; |
} |
} |
// if previous sort column is different then sort ascending |
if (ViewState["PreviousSortColumn"] != null) |
{ |
if (ViewState["PreviousSortColumn"].ToString() != linkButton.Text) |
{ |
newSortOrder = GridSortOrder.Ascending; |
} |
} |
// if previous sort column is same and previous sort order is descending then remove all sorting |
if (ViewState["PreviousSortColumn"] != null && ViewState["PreviousSortOrder"] != null) |
{ |
GridSortOrder gridSortOrder = (GridSortOrder)ViewState["PreviousSortOrder"]; |
if (gridSortOrder == GridSortOrder.Descending && ViewState["PreviousSortColumn"].ToString() == linkButton.Text) |
{ |
ViewState["PreviousSortOrder"] = null; |
ViewState["PreviousSortColumn"] = null; |
RadGrid1.DataBind(); |
e.Canceled = true; |
return; |
} |
} |
string sortOrder = GetSqlSortOrder(newSortOrder); |
switch (linkButton.Text) |
{ |
case "Name": |
RadGrid1.MasterTableView.SortExpressions.AddSortExpression("FirstName " + sortOrder); |
RadGrid1.MasterTableView.SortExpressions.AddSortExpression("LastName ASC"); |
break; |
case "Surname": |
RadGrid1.MasterTableView.SortExpressions.AddSortExpression("LastName " + sortOrder); |
RadGrid1.MasterTableView.SortExpressions.AddSortExpression("FirstName ASC"); |
break; |
} |
RadGrid1.DataBind(); |
ViewState["PreviousSortOrder"] = newSortOrder; |
ViewState["PreviousSortColumn"] = linkButton.Text; |
e.Canceled = true; |
return; |
} |
else |
{ |
ViewState["PreviousSortOrder"] = null; |
ViewState["PreviousSortColumn"] = null; |
} |
} |
else |
{ |
ViewState["PreviousSortOrder"] = null; |
ViewState["PreviousSortColumn"] = null; |
} |
} |
} |
} |
0

tmlipinski
Top achievements
Rank 1
answered on 20 May 2009, 08:23 PM
Hi,
Thank you for so detailed answer.
Yes, it works - but I can wait for the corrected RadGrid and I will wait.
BTW, I'm not sure if it is a bug - it is clearly stated in the documentation that there can be only one field name in the SortExpression. It could mean that this behaviour is by design....
Regards
Tomasz
Thank you for so detailed answer.
Yes, it works - but I can wait for the corrected RadGrid and I will wait.
BTW, I'm not sure if it is a bug - it is clearly stated in the documentation that there can be only one field name in the SortExpression. It could mean that this behaviour is by design....
Regards
Tomasz
0

Nate Pinchot
Top achievements
Rank 1
answered on 20 May 2009, 08:25 PM
Well if you don't think it's a bug then I wouldn't expect a correction? :)
Ah well. Use whatever you do or don't want to doesn't really matter to me :) GFY
Ah well. Use whatever you do or don't want to doesn't really matter to me :) GFY
0

tmlipinski
Top achievements
Rank 1
answered on 21 May 2009, 06:58 AM
Hi,
I've meant that it seems not to be an accidental programmer's error but something that this programmer planned and designed. I've just pointed out facts.
Whetever is the reason - I don't like it and think it must be changed.
Regards
Tomasz
I've meant that it seems not to be an accidental programmer's error but something that this programmer planned and designed. I've just pointed out facts.
Whetever is the reason - I don't like it and think it must be changed.
Regards
Tomasz
0

tmlipinski
Top achievements
Rank 1
answered on 06 Aug 2009, 01:33 PM
Hi, Telerik Team,
could you state whether this issue will be fixed in any of next releases?
Thanks in advance.
Regards
Tomasz
could you state whether this issue will be fixed in any of next releases?
Thanks in advance.
Regards
Tomasz