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

[Solved] Sorting strings as numerics

2 Answers 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anna
Top achievements
Rank 1
Anna asked on 12 Mar 2009, 03:19 PM
Hi,

I use Telerik RadGrid. DataSource for the grid is a web-service. One of the columns in RadGrid is a string but it contains numeric values. Though it is GridNumericColumn when I apply sorting the column is sorted as string value like 1,11,..,2, 22.
I can't change anything in datasource, including this column type.

What should I do to make the column sorted as a numeric, like 1,2,..11, 22...?

My code looks like this:

 

grid =

new RadGrid

 

 

{

 

ID =

"grdChildren",

 

AutoGenerateColumns =

false,

 

DataSourceID = ds.ID,

AllowSorting =

true

 

};

grid.MasterTableView.Columns.Add(

new GridNumericColumn

 

{

   DataField =

"StringDataField", HeaderText = "ShouldBeNumericSorting"

 

});

 
Thank you very much in advanced.

2 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 16 Mar 2009, 05:21 PM
Hi Anna,

Please review this online help topic and let me know how it goes.

Best wishes,
Georgi Krustev
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
cheburek
Top achievements
Rank 1
answered on 23 Jul 2009, 05:48 PM
I have the same problem: need to have numeric sorting for string column
I have studied your topic
Here is my code:
Private Sub RadGrid1_SortCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridSortCommandEventArgs) Handles RadGrid1.SortCommand   
      If e.SortExpression = "SpecNo" Then   
        Dim dm As New DataManagers.Specs.DMSpec()   
        Dim data = dm.Collect()   
        e.Item.OwnerTableView.DataSource = data.OrderBy(Function(r) CInt(r.NumberColumn))   
        e.Item.OwnerTableView.Rebind()   
      End If   
    End Sub   
  
    Private Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource   
      Dim dm As New DataManagers.Specs.DMSpec()   
      Dim data = dm.Collect()
      RadGrid1.DataSource = data   
    End Sub  
and aspx part here:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" 
AutoGenerateColumns="false"
      <MasterTableView AutoGenerateColumns="False" DataKeyNames="NumberColumn" 
        AllowCustomSorting="True"
        <Columns>        
<telerik:GridBoundColumn DataField="NumberColumn" HeaderText="NumberColumn" ReadOnly="true" SortExpression="NumberColumn" 
            UniqueName="NumberColumn"
          </telerik:GridBoundColumn> 
<telerik:GridTemplateColumn HeaderText="Last Updated /<br>Last Updated By" SortExpression="LastUpdated" 
            ReadOnly="true" UniqueName="LastUpdated"
            <ItemTemplate> 
              <asp:Literal ID="lblLastUpdated" runat="server" Text='<%# Bind("LastUpdated", "{0:d}") %>' /><br /> 
              <asp:Literal ID="lblLastUpdatedBy" runat="server" Text='<%# Bind("LastUpdatedBy") %>' /> 
            </ItemTemplate> 
          </telerik:GridTemplateColumn>  
</Columns> 
      </MasterTableView> 
      </ClientSettings> 
    </telerik:RadGrid> 

I have problem with LastUpdated field when it has DBNull value in datatable. It is also the same problem if to use GridBoundColumn instead of GridTemplateColumn.
After sorting using dataTable.OrderBy extension we get not a datatable but ordered list of dataRows so that is a problem.

As a simplest solution it is possible to check for null in RadGrid1.ItemDataBound handler but in this case I must replace all GridBoundColumn with GridTemplateColumn but it sounds like a 'hack'
Could you help with this please
Tags
Grid
Asked by
Anna
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
cheburek
Top achievements
Rank 1
Share this question
or