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

Sorting - Disable None

5 Answers 320 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Claude
Top achievements
Rank 1
Claude asked on 10 Apr 2015, 11:05 PM

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.

 

 

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 13 Apr 2015, 10:13 AM
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.

 
0
Abba
Top achievements
Rank 1
answered on 28 Aug 2015, 02:31 AM

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,

0
Stefan
Telerik team
answered on 28 Aug 2015, 02:15 PM
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
0
Abba
Top achievements
Rank 1
answered on 08 Sep 2015, 04:09 AM

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.

0
Stefan
Telerik team
answered on 08 Sep 2015, 06:02 AM
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
Tags
GridView
Asked by
Claude
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Abba
Top achievements
Rank 1
Share this question
or