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

Problem with custom sorting

3 Answers 287 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Francisco
Top achievements
Rank 1
Francisco asked on 18 Aug 2016, 09:05 PM

Hello Support,

  I am having trouble with custom sorting accessing values that were set using cellformatting().  I have a Gridview that is databound to a custom class,  AutoGenerateColumns = false, adding columns using grid.MasterTemplate.Columns.Add(), using CellFormatting() to populate the data on some of the columns that are not in the custom class.

Values that are assigned from the databound class show up ok, but the Values that were assigned using CellFormatting() are always null.  

How can I access the values set using CellFormatting() { e.CellElement.Text = "X"} ?

Thanks

void grid_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
        {
            foreach (var descriptor in e.Template.SortDescriptors)
            {
                if (uicomponents != null && !String.IsNullOrEmpty(descriptor.PropertyName) )
                {
                    var customcolumn = uicomponents.FirstOrDefault( ui => ui.FriendlyName.Equals(descriptor.PropertyName));
                    if (customcolumn != null)
                    {
                        int result = 0;
                        System.Diagnostics.Debug.WriteLine(descriptor.PropertyName);
 
                        try
                        {
                            //var cellValue1 = (string)e.Row1.Cells[descriptor.PropertyName].Value;
                            //var cellValue2 = (string)e.Row2.Cells[descriptor.PropertyName].Value;
 
                            var cellValue1 = (string)e.Row1.Cells[descriptor.PropertyIndex].Value;
                            var cellValue2 = (string)e.Row2.Cells[descriptor.PropertyIndex].Value;
 
                            if (cellValue1 == cellValue2)
                            {
                                result = 0;
                            }
                            else if (cellValue1 != null)
                            {
                                result = 1;
                            }
                            else if (cellValue2 != null)
                            {
                                result = -1;
                            }
                        }
                        catch
                        {
 
                        }
                        e.SortResult = result;
                        e.Handled = true;
                        return;
                         
                    }
                    else
                    {
                        //Check here if the value for cells that are databound are populated.
                        var cellValue1 = (string)e.Row1.Cells[descriptor.PropertyIndex].Value;
                        var cellValue2 = (string)e.Row2.Cells[descriptor.PropertyIndex].Value;
 
                        //This actually works.
                        System.Diagnostics.Debug.WriteLine(cellValue1 + " " + cellValue2);
                    }
                }
            }
 
            e.Handled = false;
        }

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Aug 2016, 08:33 AM
Hello Francisco,

Thank you for writing. 

Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. Hence, it is not appropriate to use the visual cell elements for the custom sorting functionality. In case you add some of the programmatically, note that you can iterate the grid rows and assign the values for these cells. Thus, the data cells will have the relevant value and it won't be necessary to use the CellFormatting event to apply the Text. In addition, you will be able to use the GridViewCellInfo. in the CustomSorting event in order to achieve the desired sort order. Additional information for the custom sorting functionality is available here: http://docs.telerik.com/devtools/winforms/gridview/sorting/custom-sorting

You can refer to the Demo application >> GridView >> Sorting >> Custom Sorting example as well.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Will
Top achievements
Rank 1
answered on 22 Nov 2017, 04:39 PM

In regards to sorting, I am setting the data source of my grid to a binding list and adding sort descriptors on the initial load. When I add a new item to the binding list, the grid is refreshed but does not maintain the sort order (Ex: On initial load, the newest records are at the top. When adding a new record, it shows up as the last row instead of first).

What's the best way to refresh the sort?

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Nov 2017, 08:56 AM
Hello, Will,

Thank you for writing.  

If you use the basic sorting that RadGridView supports, the rows will be kept always sorted. Hence, if you add a SortDescriptor it will sort the available records. Then, when you add a new record, it will be placed in the correct place considering the sort order. However, if you use the custom sorting approach in order to force the CustomSorting event it is necessary to refresh the MasterTemplate.

If you are still experiencing any further difficulties, feel free to submit a support ticket where you can provide additional information and a sample project demonstrating the undesired behavior. Thus, Telerik support will gladly assist you.  

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Francisco
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Will
Top achievements
Rank 1
Share this question
or