如何通过点击列标题编程地对DataGridView进行排序

3
我有一个Windows窗体DataGridView,需要在单击特定列标题时应用自定义排序,并在再次单击列标题时反转排序。
我将实现自己的排序算法,但不清楚如何将列标题单击与事件连接或触发,然后跟踪上一次应用于该列的排序方式,以便我可以反转排序过程。
DataGridView的数据是通过列表提供的,并且行是通过myList.Rows.Add(string_1, string_2, string_3)添加到DataGridView中。
请注意,这不是我的代码,我只被要求为每列实现自定义排序。
我已经在线搜索过了,但没有找到示例或解释。
有人能为我提供样例代码,或指点我一个展示如何实现此功能的好网站吗?
先感谢您,
Marwan

2
谁给原帖点了踩,请留下一些评论,帮助他下次做得更好? - King King
2个回答

6
这是一个可行的解决方案,我相信还有其他解决方案可以找到,并希望其他人能够加入并提供它们。您只需向 DataGridViewSortCompare 事件处理程序添加自定义代码,并在此处执行自己的比较函数,该比较函数具有2个参数并返回-1、0或1。
private void dataGridView_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
   if(e.Column == [your desired column]){
     e.Handled = true;
     e.SortResult = Compare(e.CellValue1, e.CellValue2);
   }
}
//Here is my compare function, it simply reverts the normal comparison result
private int Compare(object o1, object o2)
{
   return -o1.ToString().CompareTo(o2.ToString());
}

要测试它,您只需要在一个列的DataGridView中添加3行,例如a,b,c。通常升序(由ColumnHeader上的向上三角形表示)为a,b,c,但使用上述Compare函数,它将是c,b,a。同样,降序(由ColumnHeader上的向下三角形表示)为c,b,a,但使用上述Compare函数,它将是a,b,c
您可以添加更多自己的比较函数,并对您喜欢的每个列使用它们。我认为重要的是如何定义这些函数,我不知道为什么您想这样做,因为默认比较已经足够好了。

我会看一下,并尽快回复您。至于排序的原因,它是根据在DataGridView中呈现的数据的独特性而定的。 - Marwan مروان
这不是我最初想要的,但最终它对我所需的工作非常好。非常感谢您花时间解释它。它真的帮了我很多。 - Marwan مروان
1
自从我看到了这样优秀的解决方案以来已经很长时间了,如果可以的话我会投+10票。非常感谢! - Jonathan Applebaum

2
只要您使用这个线程中的排序方法:如何通过两列对 DataGridView 进行排序,就可以简单地跟踪用户正确顺序点击的最新的 N 个列。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private readonly Stack<int> _stack = new Stack<int>();

        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // Column history
            _stack.Push(e.ColumnIndex);

            // Number of columns to track
            int columns = 3;

            // Build sort string
            int[] array = _stack.Distinct().ToArray();
            var builder = new StringBuilder();
            for (int index = 0; index < array.Length; index++)
            {
                int i = array[index];
                if (index >= columns)
                {
                    break;
                }

                DataGridViewColumn gridViewColumn = dataGridView1.Columns[i];
                string sort = null;
                switch (gridViewColumn.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Ascending:
                        sort = "ASC";
                        break;
                    case SortOrder.Descending:
                        sort = "DESC";
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                builder.AppendFormat("{0} {1}, ", gridViewColumn.Name, sort);
            }
            string s = builder.ToString();
            s = s.Remove(s.Length - 2);
            Console.WriteLine(s);
        }
    }
}

谢谢!我不确定这是否适用于我的情况,但我会尽快查看。 - Marwan مروان

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接