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

RadGrid Sorting on Numeric String Values

1 Answer 423 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Katie Arnott
Top achievements
Rank 1
Katie Arnott asked on 02 Aug 2012, 01:56 PM
Hi Team,

I understand that a column will sort alpha when DataType String, and numerically when DataType Int, Double.
Is there a way for me to change my DataType string to Int in my SortCommand EventHandler? (I need to have column initially as DataType String for values <10, I display '< 10'.   How can I make this column sort numerically?

Here is my current SortCommand Event Handler and my Initial RadGrid Sort settings

 
public virtual void AssignSortSettingsAndSortCommandEvent()
        {
            GridAdapter.AllowSorting = GlobalConstantsSharedWebUI.RadControls.RadGridAllowSorting;
            GridAdapter.MasterTableView.SortExpressions.AllowNaturalSort = GlobalConstantsSharedWebUI.RadControls.AllowNaturalSort;
             
            GridAdapter.AddGridSortCommandEventHander(OnGridSortCommand);
        }
 
        public virtual void OnGridSortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
        {
            GridSortExpression sortExpr = new GridSortExpression();
            var radGrid = (source as RadGrid);
 
            var col = radGrid.MasterTableView.GetColumn(string.Concat(e.SortExpression, "_UniqueName")) as GridBoundColumn;
            col.DataFormatString = "{0:N}";
            col.DataType = typeof(Double);
             
            switch (e.OldSortOrder)
            {
                case GridSortOrder.None:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = GridSortOrder.Descending;
                     
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
                case GridSortOrder.Ascending:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = radGrid.MasterTableView.AllowNaturalSort ? GridSortOrder.None : GridSortOrder.Descending;
 
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
                case GridSortOrder.Descending:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = GridSortOrder.Ascending;
 
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
            }
 
            e.Canceled = true;
            radGrid.Rebind();
        }

 

 

 

 

 

 




1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 07 Aug 2012, 11:00 AM
Hi Katie,

RadGrid does not convert between data types. If you specify a DataType for your column, the grid will try to cast the data values of that column to the specified data type. No conversion is performed, though. Therefore, you need to specify a data type to which data values for the specified column can be cast. If you omit to specify the DataType for a column, the type of the respective data field or property from the data source will be assigned.
In your case you could have one additional field int the DataBase which contains the same values with the needed DataType for sorting.



All the best,
Maria Ilieva
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
Ajax
Asked by
Katie Arnott
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Share this question
or