This is a migrated thread and some comments may be shown as answers.

How to do Sorting based on multiple fields

8 Answers 1036 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ashwane
Top achievements
Rank 1
ashwane asked on 11 Jan 2011, 04:42 PM
Hi,

I am facing a problem with the sorting of column having multiple data records with different datatypes 

My radGrid contains a template column with two fields mentioned below:- 

  Field Name                      DataType
 
  StartedOn                         DateTime
  StartTime                         String 

  <telerik:GridTemplateColumn HeaderText="Started On" ShowSortIcon="true" SortExpression="StartedOn" UniqueName="StartedOn">
 <HeaderStyle Width="175px" HorizontalAlign="Center" VerticalAlign="Middle" BorderStyle="None" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
 <ItemTemplate>
 <asp:Label ID="lblStartDate" Text='<%# ((ProductionRun) Container.DataItem).StartedOn.ToString("MM/dd/yyyy")== "01/01/0001" ? "" : ((ProductionRun) Container.DataItem).StartedOn.ToString("MM/dd/yyyy")  %>' runat="server" ></asp:Label>&nbsp;
  <asp:Label ID="lblStartTime" Text='<%# DataBinder.Eval(Container.DataItem, "StartTime")   %>' runat="server" ></asp:Label>  
  </ItemTemplate>
  </telerik:GridTemplateColumn>


The above code works fine if sorts only on StartedOn field.
I tried to modify the sort expression as  SortExpression="StartedOn,StartTime" but it is giving me error at runtime

But, the requirement is to invoke the sorting based on combination of StartedOn and StartTime.


Can anyone help me to how to accomplish this ??

Thanks In Advance

Ashwani



8 Answers, 1 is accepted

Sort by
0
ashwane
Top achievements
Rank 1
answered on 13 Jan 2011, 10:44 AM
Anyone has any ideas ???

Help!!!!!!!!

Thanks
Ashwani
0
Radoslav
Telerik team
answered on 14 Jan 2011, 10:22 AM
Hello Ashwane,

Could you please try using the following markup and let me know if it helps you:
<HeaderTemplate>
      <table>
            <tr>
                <td style="width: 50%">
<asp:LinkButton ID="btnStartedOn" Text="StartedOn" CommandName='Sort' CommandArgument='StartedOn' runat="server" /></td>
               <td style="width: 50%">
<asp:LinkButton CssClass="Button" ID="btn StartTime" Text=" StartTime"
CommandName='Sort' CommandArgument='StartTime' runat="server" /></td>
            </tr>
       </table>
   </HeaderTemplate>

Also the same approach is used into the following online example:
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

Greetings,
Radoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
ashwane
Top achievements
Rank 1
answered on 14 Jan 2011, 11:07 AM
Hi Radoslav,
 
Thanks for replying.

It is working fine.But I dont want two link buttons in a single template.

I need a single linkbutton or header on which both StartedOn,StartTime sorting works together.

can i make sort expression like this  SortExpression="StartedOn + StartTime" ???


Regards,
Ashwani




0
Radoslav
Telerik team
answered on 19 Jan 2011, 02:31 PM
Hello Ashwane,

The RadGrid does not support build in sorting by more than one column at the same time, however you could try building your own sorting expressions and apply them to the grid when the column header is clicked. The other way to achieve the desired functionality is to use a custom sort criteria when user clicks on column header.

I hope this helps.

All the best,
Radoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
iwelina
Top achievements
Rank 1
answered on 21 Dec 2011, 02:58 PM
I have the same task as ashwane, I have a  template column which presenting "firstName lastName" and should sort by firstName, lastName. I tried custom sorting as OnSortCommand placed the following code

GridTableView tableView = e.Item.OwnerTableView;
            if( e.SortExpression == "firstname, lastname" )
            {
                 
 
                GridSortOrder newSortOrder = e.NewSortOrder;
                GridSortExpression expression1 = new GridSortExpression();
                expression1.FieldName = "first_name";
                expression1.SortOrder = newSortOrder;
 
                GridSortExpression expression2 = new GridSortExpression();
                expression2.FieldName = "last_name";
                expression2.SortOrder = newSortOrder;
 
                tableView.SortExpressions.Add( expression1 );
                tableView.SortExpressions.Add( expression2 );
                tableView.Rebind();
            }


The problem comes from the databinding way - with NeedDataSource it doesn'work. If I use Object DataSource it's ok, but in some cases I cannot use  Object DataSource. Do you have a suggestion what to do in this case?

Thanks, Iva 
0
Radoslav
Telerik team
answered on 27 Dec 2011, 09:53 AM
Hi Iva,

I am sending you a simple example which demonstrates the desired functionality. Please check it out and let me know if it helps you.

Looking forward for your reply.

Greetings,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
iwelina
Top achievements
Rank 1
answered on 13 Jan 2012, 02:20 PM
Hi Radoslav,
 
Thanks for replying.

But that is not what I need. I don't want multiple column sort. I just need  when sorting column which represents concatenation of firstname lastname, the sort to be applied over the full name, but data for the name comes from my data source  in two fields - first_name, last_name and that's why I placed them both in sort expression, separated by comma. Is is possible this to be done?

Thanks, Iva
0
Radoslav
Telerik team
answered on 17 Jan 2012, 10:10 AM
Hi Iva,

Unfortunately the RadGrid does not support the desire functionality. To achieve it you need to add additional column into your database which contains the concatenated fields. Then you could sort by this column. Please give it try and let me know if you experience any problems.

Regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
ashwane
Top achievements
Rank 1
Answers by
ashwane
Top achievements
Rank 1
Radoslav
Telerik team
iwelina
Top achievements
Rank 1
Share this question
or