在DataGridView中排序所选行 [英] Sorting selected rows in DataGridView

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

问题描述

我在Winforms应用程序中有一个DataGridView。我想选择一组行,并按列(Timestamp)排序这些行。



其余的行应保持原样。 。
可以使用DGV的sort属性来实现这一点。



谢谢

解决方案


可以使用DGV的排序
属性来实现




DataGridView 的.80%29.aspxrel =nofollow> 排序方法更简单的排序。例如升序或降序排序和 SortOrder 属性也只是为了简单排序。



可以这个行为实施?



我认为最简单的方法是这样做:




  • 存储第一个选定项目的索引

  • DataGridView
  • $中取出要排序的项目b $ b
  • 使用LINQ对列表进行排序

  • 添加两个列表,并在第一步存储的索引中添加排序列表。



    • 但是,如果您选择了彼此不相关的项目,则需要考虑如何处理,例如,如果选择索引 {1 ,3,6,9} 您可能会将其附加到索引 1



      编辑



      好吧,我玩了一下,想出了一个可以实现的方法。它不是很优化,但你会得到我的意思。



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

        static IEnumerable< T> SortSelectedIndices< T>(
      IEnumerable< T>值,
      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)继续;

      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};

      打印(未分类);

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

      var sorted = SortSelectedIndices(未排序,选择,排序);

      打印(排序);
      }

      并打印出以下内容:

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

      我只是用一个简单的方法来打印这个到控制台:

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

      所以你可以做的是有一个排序 - 按钮,当按下您调用 SortSelectedIndices ,然后在完成后重新绑定列表。 记住我没有对这段代码进行概要分析或重构,它可能没有你想要的那样快,我只想给你一个想法,以便你能够实现这个解决方案。


      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)...

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

      Thanks

      解决方案

      Can this be done using the sort property of DGV

      No

      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:

      • Store the index of the first selected item
      • Take out the items that you want to sort from the DataGridView
      • Sort the list using LINQ
      • Append the two lists and add the sorted list at the index stored in the first step.

      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.

      Edit

      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.

      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;
      }
      

      Then I call it like this:

      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);
      }
      

      And this prints out the following:

      { 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(" }");
      }
      

      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天全站免登陆