Problem with custom sorting

2 posts, 0 answers
  1. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 18 Aug Link to this post

    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;
            }

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 22 Aug Link to this post

    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 column 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.Value 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.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top