对 DataGridView 中的选定行进行排序 [英] Sorting selected rows in DataGridView

查看:32
本文介绍了对 DataGridView 中的选定行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Winforms 应用程序中有一个 DataGridView.我想在其中选择一组行并按列(时间戳)对这些行进行排序...

I have a DataGridView in a Winforms application. I want to select a set of rows in it and sort those rows by a column (Timestamp)...

其余的行应保持原样..可以使用 DGV 的 sort 属性完成此操作

The rest of the rows should remain as they originally were.. Can this be done using the sort property of DGV

谢谢

推荐答案

这可以使用排序来完成吗DGV的属性

Can this be done using the sort property of DGV

排序DataGridView 上的 > 方法 用于更简单的排序.例如升序或降序排序,SortOrder 属性也仅用于简单"排序.

The Sort method on the DataGridView is used for a bit more simple sorting. Such as Ascending or Descending sorting and the SortOrder property is also just for "simple" sorting.

这个行为可以实现吗?当然.

Can this behavior be implemented? Sure.

我认为最简单的方法是:

I think the easiest way to do this, is this:

  • 存储第一个选定项的索引
  • DataGridView中取出要排序的项目
  • 使用 LINQ 对列表进行排序
  • 附加两个列表,并在第一步存储的索引处添加已排序的列表.

但是,如果您选择了彼此不跟随的项目,您需要考虑如何处理,例如如果您选择 index { 1, 3, 6, 9 } 您可能会仍然希望将此附加到索引 1.

However you need to think about how you want to handle if you selecte items that are not followed by each other, for instance if you select index { 1, 3, 6, 9 } you might stil lwant to append this to index 1.

编辑

好的,所以我稍微尝试了一下,并想出了一种可以实现它的方法.它不是很优化,但你会明白我的意思.

Okay so I played around with this a little bit and came up with a way that you can implement this. It's not very optimized but you'll get the idea on how I meant.

首先这是我使用的 SortSelectedIndices 方法:

First of all this is my SortSelectedIndices-method that I use:

static IEnumerable<T> SortSelectedIndices<T>(
    IEnumerable<T> values, 
    IEnumerable<int> selectedIndices, 
    Func<IEnumerable<T>, IEnumerable<T>> sort)
{
    var selectedValues = new List<T>();

    for (var i = 0; i < selectedIndices.Count(); i++)
        selectedValues.Add(values.ElementAt(selectedIndices.ElementAt(i)));

    var sortedList = sort(selectedValues);

    var finalList = new List<T>();

    var startPositionFound = false;
    for(var i = 0; i < values.Count(); i++)
    {
        if (selectedIndices.Contains(i))
        {
            if (startPositionFound) continue;

            startPositionFound = true;
            finalList.AddRange(sortedList);
        }
        else
            finalList.Add(values.ElementAt(i));
    }

    return finalList;
}

然后我这样称呼它:

static void Main(string[] args)
{
    var unsorted = new[] {3, 5, 6, 1, 2, 87, 432, 23, 46, 98, 44};
    var selected = new[] {1, 4, 7};

    Print(unsorted);

    var sort = new Func<IEnumerable<int>, IEnumerable<int>>(
        (x) => x.OrderBy(y => y).ToList());

    var sorted = SortSelectedIndices(unsorted, selected, sort);

    Print(sorted);
}

这会打印出以下内容:

{ 3,5,6,1,2,87,432,23,46,98,44 }
{ 3,2,5,23,6,1,87,432,46,98,44 }

我只是在这里使用一个简单的方法将其打印到控制台:

I am just using a simple method here to print this out to the console:

static void Print<T>(IEnumerable<T> values)
{
    Console.Write("{ ");
    Console.Write(string.Join(",", values));
    Console.WriteLine(" }");
}

所以你可以做的是有一个排序"按钮,当它被按下时你调用SortSelectedIndices,然后在你完成后重新绑定列表.记住我没有分析或重构这段代码,它可能没有你喜欢的那么快,我只是想告诉你你可以做什么来实现解决方案.

So what you can do is to have a "sort"-button, when it's pressed you invoke SortSelectedIndices and then rebind the list when you're done. Remember I have not profiled or refactored this code, it might not be as fast as you like, I just want to give you an idea on what you can do to acheive the solution.

这篇关于对 DataGridView 中的选定行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆