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

Programmatically sort multiple columns

6 Answers 376 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 22 Mar 2011, 09:11 PM
Hi, basically I want to simulate the Shift + click to sort multiple columns. For example, if the user click on Column A to do an ascending sort, I want to also sort Column B ascending.
I added the sort descriptors in the designer and it works on startup, but as soon I click on Column A or B, the original sort descriptors are lost.
Any suggestions?

Thanks,
Andy

6 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 25 Mar 2011, 11:21 AM
Hello Andy,

There is no easy way to do this, but this example will do everything you need:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<Person> persons;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.GridBehavior = new CustomGridBehavior();
 
        persons = new List<Person>();
 
        for (int i = 0; i < 20; i++)
        {
            if (i % 4 == 0)
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "L'Person " + i,
                });
            }
            else
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "Person " + i,
                });
            }
        }
 
        radGridView1.DataSource = persons;
    }
 
    private class CustomGridBehavior : BaseGridBehavior
    {
        public override bool OnMouseDown(MouseEventArgs e)
        {
            var cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridHeaderCellElement;
            if (cell != null)
            {
                Keyboard.HoldShift();
            }
 
            return base.OnMouseDown(e);
        }
 
        public override bool OnMouseUp(MouseEventArgs e)
        {
            if (Keyboard.ShiftPressed)
            {
                Keyboard.ReleaseShift();
            }
 
            return base.OnMouseUp(e);
        }
 
        public override bool ProcessKeyPress(KeyPressEventArgs keys)
        {
            if (keys.KeyChar == (char)Keys.Space)
            {
                if (this.GridControl.CurrentRow != null && this.GridControl.CurrentRow.DataBoundItem != null)
                {
                    var data = this.GridControl.CurrentRow.DataBoundItem as Person;
                    if (data != null)
                    {
                        data.Selected = !data.Selected;
                        this.GridControl.CurrentRow.InvalidateRow();
                    }
                }
            }
 
            return base.ProcessKeyPress(keys);
        }
    }
 
    private static class Keyboard
    {
        const int VK_SHIFT = 0x10;
        const uint KEYEVENTF_KEYUP = 0x2;
        static bool shiftPressed = false;
 
        public static bool ShiftPressed
        {
            get
            {
                return Keyboard.shiftPressed;
            }
        }
 
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
 
        public static void HoldShift()
        {
            if (!shiftPressed)
            {
                keybd_event((byte)VK_SHIFT, 0, 0, 0);
                shiftPressed = true;
            }
        }
 
        public static void ReleaseShift()
        {
            if (shiftPressed)
            {
                keybd_event((byte)VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
                shiftPressed = false;
            }
        }
    }
}
 
public class Person
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public bool Selected
    {
        get;
        set;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Accepted
Jack
Telerik team
answered on 25 Mar 2011, 12:25 PM
Hello Andy,

Thank you for this question. You can use the SortOrder property of GridViewDataColumn to simulate the Shift key. Here is an example:
this.radGridView1.Columns[0].SortOrder = RadSortOrder.Ascending;
this.radGridView1.Columns[1].SortOrder = RadSortOrder.Descending;

I hope this helps. If you have further questions, do not hesitate to write us.

All the best,
Jack
the Telerik team
0
Andy
Top achievements
Rank 1
answered on 25 Mar 2011, 07:22 PM
Emanuel, thank you very much for your sample code. It's very close to what I need, and shows new information about the grid that I didn't know before.

Jack, your solution works. I just have to figure out which events to change the sort order. I'm currently using MouseDown to trap the column sorting event, and change the other column SortOrder on MouseUp.

Andy
0
Jack
Telerik team
answered on 30 Mar 2011, 03:58 PM
Hello Andy,

Thank you for writing us back. You can handle the SortChanged event in this case. Please consider the following code:
void radGridView1_SortChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    if (this.radGridView1.SortDescriptors.Contains("City"))
    {
        this.radGridView1.Columns["Country"].SortOrder = this.radGridView1.Columns["City"].SortOrder;
    }
    else
    {
        this.radGridView1.Columns["Country"].SortOrder = RadSortOrder.None;
    }           
}

Regards,

Jack
the Telerik team
0
pawan
Top achievements
Rank 1
answered on 22 Dec 2016, 06:14 AM

I have the same requirement for Kendo UI Grid.

Any suggestion, please.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Dec 2016, 06:42 AM
Hello Pawan,

Thank you for writing.  

I would like to note that this forum is related to the Telerik UI for WinForms suite. If you have any questions regarding other Telerik products, feel free to post your queries in the relevant forum: http://www.telerik.com/forums

Thank you for your understanding.

Regards,
Dess
Telerik by Progress
Try our brand new, jQuery-free Angular 2 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
Andy
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jack
Telerik team
Andy
Top achievements
Rank 1
pawan
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or