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

GridViewDecimalColumn not sorting correctly

3 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 30 Oct 2012, 09:04 PM
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. 


3 Answers, 1 is accepted

Sort by
0
Dave
Top achievements
Rank 1
answered on 30 Oct 2012, 09:37 PM
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;
    }
}
0
Stefan
Telerik team
answered on 02 Nov 2012, 11:23 AM
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.
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Nov 2012, 12:56 PM
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
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Dave
Top achievements
Rank 1
Stefan
Telerik team
Emanuel Varga
Top achievements
Rank 1
Share this question
or