Telerik Rad Grid Sorting Not Working for Numeric String and Date String Columns

As i have posted my last blog post for sorting Telerik Rad Grid Sorting. That was a normal sorting but if you want to sort numeric string column or date string column in that case you would not get desired result. For example: Values are 5, 31, 22, 41, 76, 8, 6
When we click on column header for sorting then sorted values are 22, 31, 41, 5, 6, 76, 8
 But expected result is 5, 6, 8, 22, 31, 41, 76

So for take over this problem follow below example :

For example we will add a numeric string column and a date string column to my last blog post Telerik Rad Grid Sorting.

 .aspx Code :

<telerik:GridBoundColumn DataField="Date" HeaderText="Date" DataType="System.DateTime"
  SortExpression="Date"></telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Number" HeaderText="Number" DataType="System.Int32"
   SortExpression="Number"></telerik:GridBoundColumn>


Also when you bind this grid you have to bind these column with date entity and numeric entity respectively.

.aspx.cs Code :

     public class GridEntity
        {
            public GridEntity()
            {
                lstGridItems = new List<GridItemEntity>();
            }

            public List<GridItemEntity> lstGridItems { get; set; }
        }

        public class GridItemEntity
        {
            public string Name{ get; set; }
            public string Address{ get; set; }
            public DateTime? Date{ get; set; }
            public int Number{ get; set; }
        }
 

      private void BindGridData()
       {
           GridEntity entity = new GridEntity();
            entity.lstGridItems.Add(new GridItemEntity
            {
                Name = "Abc",
                Address = "NY",
                Date = System.Convert.ToDateTime("11/11/2013 12:00:00 AM"),

                Number = Int32.Parse("19")
            });

            entity.lstGridItems.Add(new GridItemEntity
            {
                Name = "Def",
                Address = "CY",
                Date = System.Convert.ToDateTime("04/12/2012 12:00:00 AM"),                

                Number = Int32.Parse("15")
            });

            RadGrid1.DataSource = entity.lstGridItems; 

        }

For Bind grid with data use above function BindGridData within Need Data Source grid's event. Also write "RadGrid1.DataBind();" on Page Load Event.

Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell