阅读全文并下载例子 :http://www.sufeinet.com/forum.php?mod=viewthread&tid=190

以前不都是用table直接绑定DataGridView的,没有出现过不能排序的问题,初试List结果发现不管怎么样都不能实现排序的功能,有朋友说

DataGridView每一列都有个Sortable,默认Automatic,改成NotSortable了,结果怎样,还是不行啦。

还有朋友说, 你可以拖一个bindingsource控件. bindingsource.datasource=泛型集合 datagridview.datasource=bindingsource;

我发现也是不行,那要怎么办呢?查一下资料才知道

用泛型会失去DateTable的特性,要实现System.Collections.Generic.IComparer<T> 才能实现排序

没有办法只能实现 一把了

看一下下面的代码吧, 基本 是这样的

代码

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;

namespace BaseFunction
{
    class ObjectPropertyCompare<T> : System.Collections.Generic.IComparer<T> 
    {
        private PropertyDescriptor property;
        private ListSortDirection direction;

public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
        {
            this.property = property;
            this.direction = direction;
        }

#region IComparer<T>

/// <summary>
        /// 比较方法
        /// </summary>
        /// <param name="x">相对属性x</param>
        /// <param name="y">相对属性y</param>
        /// <returns></returns>
        public int Compare(T x, T y)
        {
            object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
            object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);

int returnValue;

if (xValue is IComparable)
            {
                returnValue = ((IComparable)xValue).CompareTo(yValue);
            }
            else if (xValue.Equals(yValue))
            {
                returnValue = 0;
            }
            else
            {
                returnValue = xValue.ToString().CompareTo(yValue.ToString());
            }

if (direction == ListSortDirection.Ascending)
            {
                return returnValue;
            }
            else
            {
                return returnValue * -1;
            }
        }

public bool Equals(T xWord, T yWord)
        {
            return xWord.Equals(yWord);
        }

public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }

#endregion
    }
}

在实现了这个接口之后还不能急,我们还要来写一个SortableBindingList <T> :BindingList <T> 的类用来绑定数据

基本实现

代码

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace BaseFunction
{
    public class BindingCollection<T> : BindingList<T>
    {
        private bool isSorted;
        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;

protected override bool IsSortedCore
        {
            get { return isSorted; }
        }

protected override bool SupportsSortingCore
        {
            get { return true; }
        }

protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirection; }
        }

protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortProperty; }
        }

protected override bool SupportsSearchingCore
        {
            get { return true; }
        }

protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List<T> items = this.Items as List<T>;

if (items != null)
            {
                ObjectPropertyCompare<T> pc = new ObjectPropertyCompare<T>(property, direction);
                items.Sort(pc);
                isSorted = true;
            }
            else
            {
                isSorted = false;
            }

sortProperty = property;
            sortDirection = direction;

this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

protected override void RemoveSortCore()
        {
            isSorted = false;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
        //排序
        public void Sort(PropertyDescriptor property, ListSortDirection direction)
        {
            this.ApplySortCore(property, direction);
        }
    }
}

现 在应该流到怎么使用了,其实很简单

直接

 BindingCollection<object > objList = new BindingCollection<object>();
 objList =你的结果集;
 this.dataGridView1.DataSource = objList;

但是现在是问题是我的之前用的是List,不想改,而且调用的是Dll,人家返回的就是一个List,我没有办法改成BindingCollection<object >啊。

想了半天还是想出来了,只是不知道 在性能和别的方面怎么样,所以把代码发上来大家讨论一下

我是这样实现 的

代码

 //可以实现排序的类
            BindingCollection<historyorderInfo> objList = new BindingCollection<historyorderInfo>();
            //加载数据
            foreach (historyorderInfo item in tmpList)
            {
                objList.Add(item);
            }
            dgvhistory.DataSource = objList;

这里的tmpList就是我之前使用的系统原本的List,我是使用了 foreach 把原来的数据导入到BindingCollection中的。

这样的确定是可以实现 我想要的效果的。不知道这样做有什么不到之处。希望能得到高人的指点啊,呵呵

转载于:https://www.cnblogs.com/sufei/archive/2010/02/04/1663125.html

解决DataGridView绑定List后不能排序的问题相关推荐

  1. 解决 WPF 绑定集合后数据变动界面却不更新的问题(使用 ObservableCollection)

    解决 WPF 绑定集合后数据变动界面却不更新的问题 独立观察员 2020 年 9 月 9 日 在 .NET Core 3.1 的 WPF 程序中打算用 ListBox 绑定显示一个集合(满足需求即可, ...

  2. 解决 WPF 绑定集合后数据变动界面却不更新的问题

    解决 WPF 绑定集合后数据变动界面却不更新的问题 独立观察员 2020 年 9 月 9 日 在 .NET Core 3.1 的 WPF 程序中打算用 ListBox 绑定显示一个集合(满足需求即可, ...

  3. DataGridView绑定数据源后添加行

    在已经绑定数据源时,无法以Add的方式方式添加行,会报错 解决方法一: DataRow dr =((DataTable)dataGridView1.DataSource).NewRow; ((Data ...

  4. c# easyui 赋值_C# DataGridView绑定数据源的方法

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...

  5. C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题

    C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题 参考文章: (1)C# Winfrom DataGridView DataSource绑定数据源后-- ...

  6. WinForm DataGridView 绑定泛型List(ListT)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  7. 章鱼哥出品—VB.NET DataGridView绑定数据源 quot;与货币管理器的位置关联的行不能设置为不可见quot; 问题的解决...

    DtaGridView绑定数据源后.假设想让数据条件显示的话,直接使用  My_Row.Visible = False就会出错.错误类型是 "与货币管理器的位置关联的行不能设置为不可见&qu ...

  8. 《Python Cookbook 3rd》笔记(4.15):顺序迭代合并后的排序迭代对象

    顺序迭代合并后的排序迭代对象 问题 你有一系列排序序列,想将它们合并后得到一个排序序列并在上面迭代遍历. 解法 heapq.merge() 函数可以帮你解决这个问题.比如: >>> ...

  9. MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)

    MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题) 1.背景知识 1.1 为什么会有碎片 MySQL 中 insert 与 update 都可能导致页分裂,这样就存在碎片 ...

最新文章

  1. 数据同步的终极解决方案,阿里巴巴开源的Canal框架当之无愧!!
  2. mysql主从复制缺陷_mysql主从复制及遇到的坑
  3. 用例图中的Actor(参与者)一定是人吗?
  4. 这些是实际面试中遇到的面试题
  5. mysql全拼_Mysql中取得汉字的全拼、拼音首字母
  6. 低代码Web应用程序构造方法-ASP.NET Core 2.2单页应用程序(SPA)
  7. Retrofit2源码分析(一)
  8. L1-031. 到底是不是太胖了-PAT团体程序设计天梯赛GPLT
  9. 规则引擎--规则引擎构成重点
  10. 寻找二叉树最小叶子节点值
  11. 简单的P2P电影下载加速,(类似迅雷下载电影P2P加速)
  12. echarts实现3D饼图
  13. unity控制相机移动
  14. APK Multi-Tool(反编译工具)教程
  15. 为 windows cmd 设置代理
  16. python输入一个三位整数、输出三位数之和_编写程序,从键盘输入一个3位的正整数,输出它的百位数,十位数和个位数,并且计算它的和...
  17. 青岛理工大学计算机学院王德兴,现任领导
  18. 生产者消费者2.0(lock)
  19. 模拟退火算法(惩罚函数法求约束优化问题)
  20. mac 蓝牙搜索不到SONY WI-1000X 耳机型号

热门文章

  1. Java变长数组笛卡尔积_Java 8中的流作为流的笛卡尔积(仅使用流)
  2. python float 精度_浅谈Python里面小数点精度的控制
  3. python中history()_keras中的History对象用法
  4. easyexcel 动态列_easyexcel动态表头列导出SequenceDiagram 阅读源码事半功倍
  5. 机箱硬盘指示灯不亮_安钛克DF600 FLUX机箱:FLUX平台第一款机箱,为全民电竞热“降温”...
  6. 光纤收发器和协议转换器之间有哪些区别?
  7. 什么叫光端机?视频光端机的分类具体有哪些?
  8. 如何正确使用工业级交换机?
  9. 如何预防光纤光缆布线中的雷击伤害
  10. 【渝粤教育】国家开放大学2018年春季 3780-21T燃气设备操作与维护 参考试题