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

SortExpressions not sorting grid

4 Answers 735 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sophie
Top achievements
Rank 2
Sophie asked on 05 Feb 2013, 05:09 PM
Hello, I have an issue with the sort expression feature in the RadGrid.
As said in several post, i'm using the advanced databinding with NeedDataSource to make the sorting works correctly.

This is my grid definitions:
<telerik:RadGrid ID="MyRadGrid" runat="server"
 AllowSorting="true"
 EnableHeaderContextMenu="true"
 Width="900px"
 CssClass="RadGridCustomClass">
                 
  <ClientSettings AllowColumnsReorder="true"
    ReorderColumnsOnClient="true"
    ColumnsReorderMethod="Reorder"
    AllowColumnHide="true" >
      <Resizing AllowColumnResize="true"
        EnableRealTimeResize="true"
        ResizeGridOnColumnResize="true"
        ClipCellContentOnResize="true"
        AllowRowResize="false" />
 </ClientSettings>
             
  <HeaderContextMenu EnableRoundedCorners="true"
    EnableShadows="true"
    OnItemClick="MyRadGrid_HeaderContextMenuClick" />
             
  <MasterTableView AllowMultiColumnSorting="false" 
    AllowCustomSorting="true"
    AllowCustomPaging="true"
    TableLayout="Fixed"
    AutoGenerateColumns="false"
    EnableHeaderContextMenu="true"
    EnableViewState="false" >
    
  <Columns>
    <telerik:GridBoundColumn DataField="ID"
      SortExpression="ID" HeaderText="ID"
      DataType="System.Int32" />
    <telerik:GridBoundColumn DataField="Name"
      SortExpression="Name" HeaderText="Name"
      DataType="System.String" />
    <telerik:GridNumericColumn DataField="UnitPrice" 
      SortExpression="UnitPrice" HeaderText="Unit price"
      DataType="System.Decimal" />
    <telerik:GridBoundColumn DataField="Date"
      SortExpression="Date" DataFormatString="{0:d}" HeaderText="Date"
      DataType="System.DateTime" />
    <telerik:GridCheckBoxColumn DataField="Discontinued"
      SortExpression="Discontinued" HeaderText="Discontinued"
      DataType="System.Boolean" />
  </Columns>
</MasterTableView>
</telerik:RadGrid>

 Pretty simple grid with simple data i load from a list for this test purpose.
On load, i check if the dataSource is nothing, then call Rebind() to call NeedDataSource()
Once its called, i bind my grid with data.

I got this on load....
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If MyRadGrid.DataSource Is Nothing Then
    MyRadGrid.Rebind()
  End If
End Sub
 
Private Sub MyRadGrid_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles MyRadGrid.NeedDataSource
  Dim list2 As New List(Of MyBusinessObject)()
  For j As Integer = 0 To 5
    Dim Product1 As New MyBusinessObject()'Some local data
    Product1.ID = j
    Product1.Name = names(j)
    Product1.UnitPrice = prizes(j)
    Product1.[Date] = dates(j)
    Product1.Discontinued = bools(j)
    list2.Add(Product1)
  Next
  MyRadGrid.DataSource = list2
End Sub
 
Public Sub MyRadGrid_HeaderContextMenuClick(ByVal sender As Object, ByVal e As RadMenuEventArgs)
  If e.Item.Value <> "SortAsc" And e.Item.Value <> "SortDesc" Then
    MyRadGrid.MasterTableView.SortExpressions.Clear()
  End If
  MyRadGrid.Rebind() 'i tried MyRadGrid.MasterTableView.Rebind()
End Sub


The sorting doesnt work well, so i use the HeaderContextMenuClick event to output what's going on...
So my grid has the expression it needs, but the sorting doesnt occur.

So i placed a couple of debug.print... Here what i get after click on "sort asc" on "Unit price" column:
******************************Page load
Page load() data source is nothing... Calling MyRadGrid.Rebind()
MyRadGrid_NeedDataSource()
******************************Page load
Page load() data source is nothing... Calling MyRadGrid.Rebind()
MyRadGrid_NeedDataSource()
'WebDev.WebServer.EXE' (Managed): Loaded 'D:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
MyRadGrid_HeaderContextMenuClick() SORT EXPRESSION: Name:UnitPrice Order:Ascending
MyRadGrid_HeaderContextMenuClick() Calling MyRadGrid.Rebind()


Rebind is supposed to call NeedDataSource right? I tried to call MyRadGrid.datasource manualy... but still no result, the grid wont sort.
Plus, i get a empty button (5-8pixel width, 2-3 pixels height) beside the header text after i call Clear sorting.. this happens only in my test, not my project.

I have no idea why this isnt working. Can anyone help me?
Thanks!

4 Answers, 1 is accepted

Sort by
0
Sophie
Top achievements
Rank 2
answered on 05 Feb 2013, 06:46 PM
After investigating in my project, clicking on the header (command arg) works and everything is sorted as wanted. But if i select the sort from the context menu, nada. Still investigating this issue.

EDIT: our team decided to remove the sort from the header context menu and keep the working one, using the attribute "sortExpression" on the GridTemplateColumn. If anyone has any idea why the context menu doesnt work, feel free to share, still folowing this issue.
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Feb 2013, 12:45 PM
Hi,

If I set the AllowCustomSorting property of the MasterTableView to false, your code is working fine at my end. Please remove the property if you don't want to customize the sorting functionality. Rebind() method will surely call the NeedDataSource event. But i am not sure why you are calling the function in the PageLoad. The NeedDataSource event will fire by default after the PageLoad event.

Thanks,
Princy.
0
Sophie
Top achievements
Rank 2
answered on 11 Feb 2013, 06:49 PM
Hey Princy.
Thanks for your advice. I will look into this and post back the result.
0
Sophie
Top achievements
Rank 2
answered on 13 Feb 2013, 08:52 PM
Hey Princy, property  AllowCustomSorting set to false fixed everything. Thanks a lot!! And i removed Rebind from Load. :)
Tags
Grid
Asked by
Sophie
Top achievements
Rank 2
Answers by
Sophie
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or