Sorting in custom column

4 posts, 0 answers
  1. Hannes
    Hannes avatar
    8 posts
    Member since:
    Oct 2006

    Posted 12 Oct 2012 Link to this post

    I have created a custom column by inheriting from GridViewDataColumn (and GridDataCellElement and BaseGridEditor and LightvisualElement)

    When used, the column is databound to an object (often an Entity Framework Entity).

    If I try to sort on the column, it doesn't work in any meaningful way, probably because the grid doesn't know how to sort the objects the column is databound to.

    Is there a way for the column to control how it is sorted ?

    I'd rather not use the custom sorting features of the grid, then I would have to implement it on all grids using the column (the column is rather generic and used all over).

    The logic I would like to implement is quite similar to logic of the DisplayMemberSort property in GridViewComboBoxColumn (sorting on something else than the value the column is databound to).

    Peeking at the code of GridViewComboBoxColumn, it seems as it is implementing this functionality by overriding the GetValue method, and looking at the GridViewDataOperation flag. However, the GetValue method is internal, so I can't do the same in my column class.

    Thanks /Hannes

  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts
    Member since:
    Sep 2012

    Posted 15 Oct 2012 Link to this post

    Hello Hannes,

    I created a demo application of custom column and I did not find any issues with the sorting operation. Here is the demo:

    using System.Data;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace Lab.Grid
    {
        public partial class GridCustomColumnForm : Form
        {
            private RadGridView gridView = new RadGridView();
     
            class MyColumn : GridViewDataColumn
            {
     
            }
     
            public GridCustomColumnForm()
            {
                InitializeComponent();
     
                gridView.Dock = DockStyle.Fill;
                gridView.Parent = this;
                gridView.AutoGenerateColumns = false;
     
                MyColumn col = new MyColumn();
                col.HeaderText = col.Name = "Id";
                col.FieldName = "Id";
                gridView.Columns.Add(col);
     
                col = new MyColumn();
                col.HeaderText = col.Name = "Name";
                col.FieldName = "Name";
                gridView.Columns.Add(col);
     
                col = new MyColumn();
                col.HeaderText = col.Name = "Sum";
                col.FieldName = "Sum";
                gridView.Columns.Add(col);
     
     
                DataTable table = new DataTable();
                table.Columns.Add("Id", typeof(int));
                table.Columns.Add("Name");
                table.Columns.Add("Sum", typeof(double));
     
                table.Rows.Add(1, "Ivan", 123.56);
                table.Rows.Add(2, "Ivan", 372.90);
                table.Rows.Add(3, "Peter", 500.00);
                table.Rows.Add(4, "George", 300.00);
                table.Rows.Add(5, "George", 600.00);
                table.Rows.Add(6, "George", 100.00);
                table.Rows.Add(7, "Enzo", 78.00);
                table.Rows.Add(8, "Enzo", 100.00);
                table.Rows.Add(9, "Enzo", 200.00);
                table.Rows.Add(10, "Enzo", 300.00);
     
                gridView.DataSource = table;
            }
        }
    }

    Please see the result in the attached screenshots. If you continue to experience the issue, please open a support ticket and send us a sample project that we can investigate locally. This will allow us to provide you with further assistance. 
     
    Kind regards,
    Julian Benkov
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.

  3. Hannes
    Hannes avatar
    8 posts
    Member since:
    Oct 2006

    Posted 17 Oct 2012 Link to this post

    Thanks for getting back so quickly.

    In your example you bind the columns to simple data types (string, int, double)
    In this case there is no problem with the sorting.

    The case I am asking about is when you bind your column to some custom class, lets call it MyDataClass, and the custom column(/cell) class contains logic that determines what is displayed in the grid, given the MyDataClass instance the cell is bound to.
    Since the grid has no way of knowing how to sort a collection of MyDataClass instances, nothing happens when the grid is sorted.

    Again, compare with GridViewComboBoxColumn. It is typically bound to an integer field, but if you specify DisplayMemberSort=true the columns is sorted not on the integers it is bound to, it is sorted according to a value that is looked up by the GridViewComboBoxColumn.

    I want to implement my own "sorting logic" similar to this.

  4. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts
    Member since:
    Sep 2012

    Posted 19 Oct 2012 Link to this post

    Hello Hannes,

    To support this functionality you must use custom sorting operation in RadGridView. More information about this topic you can find in our online documentation.

    I hope this helps.

    All the best,
    Julian Benkov
    the Telerik team

    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.

Back to Top