GridViewDecimalColumn not sorting correctly

4 posts, 0 answers
  1. Dave
    Dave avatar
    5 posts
    Member since:
    Apr 2011

    Posted 30 Oct 2012 Link to this post

    Hi everyone,

    I am hoping someone can help me solve a problem I am having with the GridViewDecimalColumn in my radGridView. I have an unbound column named "Days". This column is populated when the grid is loaded in the following way: 

    private void grid_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        if (e.Column.Name == "Days")
        {
            MyObject obj = (MyObject)e.Row.DataBoundItem;
            if (obj != null)
            {
                TimeSpan span = DateTime.Now.Date - obj.SomeDate.Date;
                e.CellElement.Value = span.Days;
            }
        }
    }

    When I try and sort on this value, the sort order does not make any sense. I get things like the following:

    14
    14
    33
    33
    36
    17
    14 (should be at the top with the other 14's)

    The sort does not follow any sort of logic and instead appears to just be sorting the numbers randomly. How can I get it to sort the numbers properly (IE ascending/descending order) 

    Thanks 

    ----- EDIT-----------

    Now that I have played with it a bit more, the grid is sorting, it is just sorting extremely slow. Moving the scrollbar up and down will force the grid to repaint and you can see the sort occurring. Why is this so slow? There are only around 160 rows in this grid. I have sorted other grids with 200+ rows and the sort is instantaneous. 


  2. Dave
    Dave avatar
    5 posts
    Member since:
    Apr 2011

    Posted 30 Oct 2012 Link to this post

    Resolved. It appears that trying to set that value in an event is not a good idea. (Maybe this event is called multiple times during a sort, which is why it is slowing down?) 

    Anyway, I got around it by doing the following after the control is loaded: 

    foreach (var row in grid.Rows)
    {
        MyObject obj = (MyObject)row.DataBoundItem;
        if (obj!= null)
        {
            TimeSpan span = DateTime.Now.Date - obj.Somedate.Date;
            row.Cells["Days"].Value = span.Days;
        }
    }
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 02 Nov 2012 Link to this post

    Hi Dave,

    Thank you for writing.

    That is correct - setting a value of a visual element (CellElement) is definitely not a good idea and yes, the event is thrown contantly. The event is used to change the apperance of the visual data cells in the grid. More information is available here: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

    You have also found the right way to populate the cells in your column. More information is available here: 
    I hope this helps.

    All the best,
    Stefan
    the Telerik team
    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Nov 2012 Link to this post

    Hello Dave,

    May I suggest a cleaner approach to this?

    First of all from my point of view, this it is the task of the data layer to populate the objects with data, as such, you should be able to use a property in the object MyObject where you can calculate this difference and bind that one to the grid.

    Also if you need to notify the grid of data changes you should use a BindingList<> and implement the INotifyPropertyChanged interface for your objects, in order to notify the UI of a data change.

    Like this you will save a lot of cpu (just check for curiosity the number of times the CellFormatting event is fired), and the code would be more maintainable.

    If you have any other questions, please let me know.

    Best Regards,
    Emanuel Varga
    Winforms MVP
Back to Top