Sorting - Disable None

6 posts, 0 answers
  1. Claude
    Claude avatar
    63 posts
    Member since:
    Apr 2009

    Posted 10 Apr 2015 Link to this post

    I want to toggle only between Ascending and Descending when the user clicks on the column header.  I do not want the None option.  Not sure what one would use the None option for anyway. That is not a sort.

     

     

  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 13 Apr 2015 Link to this post

    Hello Claude,

    Thank you for writing.

    The None sort option is quite often used, especially in scenarios where you have your have pre-sorted. However, one possible way to skip it is to override the Sort method of the header cell. Here is how to do that:
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
     
        AddGridSimple();
        radGridView1.CreateCell += radGridView1_CreateCell;
         
    }
     
    void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.CellType == typeof(GridHeaderCellElement))
        {
            e.CellType = typeof(MyHeaderCell);
        }
    }
     
    class MyHeaderCell : GridHeaderCellElement
    {
        public MyHeaderCell(GridViewColumn column, GridRowElement row) :base(column,row)
        {
     
        }
        public override void Sort(RadSortOrder sortOrder)
        {
            if (sortOrder == RadSortOrder.None)
            {
                sortOrder = RadSortOrder.Ascending;
            }
            base.Sort(sortOrder);
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridHeaderCellElement);
            }
        }
    }

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Abba
    Abba avatar
    22 posts
    Member since:
    Jan 2013

    Posted 27 Aug 2015 in reply to Stefan Link to this post

    I want the None option, but I do not have it.  I am on version 2013 Q3.  Was it put in after?  or am I doing something wrong?  If it was put in after, is there a way to have this functionality in my version?

     

    Thanks,

  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 28 Aug 2015 Link to this post

    Hi Abba,

    This functionality was added in Q2 2014. In your version to add it you need to create customer header row behavior. Here is an example:
    RadGridView radGridView1 = new RadGridView();
     
    public Form1()
    {
        InitializeComponent();
     
        this.Controls.Add(radGridView1);
        radGridView1.Dock = DockStyle.Fill;
     
        DataTable table = new DataTable();
        table.Columns.Add("col1");
        table.Columns.Add("col2");
        table.Rows.Add("value1", "value1");
        table.Rows.Add("value2", "value2");
     
        radGridView1.DataSource = table;
     
        BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
        gridBehavior.UnregisterBehavior(typeof(GridViewTableHeaderRowInfo));
        gridBehavior.RegisterBehavior(typeof(GridViewTableHeaderRowInfo), new MyHeaderRowBehavior());
    }
     
     
     
     
    class MyHeaderRowBehavior : GridHeaderRowBehavior
    {
        GridCellElement mouseDownCellElement;
        public override bool OnMouseDown(MouseEventArgs e)
        {
            mouseDownCellElement = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
            return base.OnMouseDown(e);
        }
     
        public override bool OnMouseUp(MouseEventArgs e)
        {
            GridHeaderCellElement cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridHeaderCellElement;
     
            if (e.Button == MouseButtons.Left &&
                cell != null &&
                cell == mouseDownCellElement)
            {
                RadSortOrder sortOrder = cell.SortOrder;
     
                if (sortOrder == RadSortOrder.None)
                {
                    sortOrder = RadSortOrder.Ascending;
                }
                else if (sortOrder == RadSortOrder.Ascending)
                {
                    sortOrder = RadSortOrder.Descending;
                }
                else
                {
                    sortOrder = RadSortOrder.None;
                }
     
                cell.Sort(sortOrder);
                return true;
            }
            else
            {
                return base.OnMouseUp(e);
            }
        }
    }

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Abba
    Abba avatar
    22 posts
    Member since:
    Jan 2013

    Posted 07 Sep 2015 in reply to Stefan Link to this post

    Thank you Stefan,

    While this has added the functionality, it seems to have a small side effect:
    When I resize a column, it is applying the sort to the column on the right.  Meaning if I have 2 columns (1 and 2).  When i drag the resize slider, it will then sort column 2.

    It is not a big deal and I would be sacrifice this for the no sort.  But I am curious if there is way around.

    And I appreciate your help in this thread and in the others.  You seem very knowledgeable on this control and great response time.  It is making using this control a pleasure.

  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 08 Sep 2015 Link to this post

    Hi Abba,

    Indeed, this behavior occurs as the base functionality kicks in. Can you please try the following behavior and let me know how it works for you:
    class MyHeaderRowBehavior : GridHeaderRowBehavior
    {
        bool sortedDescending = false;
     
        public override bool OnMouseUp(MouseEventArgs e)
        {
            RadSortOrder saveOrder = RadSortOrder.None;
            GridHeaderCellElement cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridHeaderCellElement;
     
            if (cell != null)
            {
                saveOrder = cell.SortOrder;
            }
     
            bool res = base.OnMouseUp(e);
     
            if (saveOrder != cell.SortOrder)
            {
                if (cell.SortOrder == RadSortOrder.Ascending)
                {
                    if (sortedDescending)
                    {
                        cell.Sort(RadSortOrder.None);
                        sortedDescending = false;
                    }
                    else
                    {
                        return res;
                    }
                }
                else if (cell.SortOrder == RadSortOrder.Descending)
                {
                    sortedDescending = true;
                    return res;
                }
            }
     
            return res;
        }
    }

    I am looking forward to your reply.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready