DataGridView不能正常显示、编辑、排序的处理

BindingList<mykgl> kgllist 这是要绑定的的list

其中mykgl是自定义的一个类。

把kgllist设置为DataGridView的DataSource后。

1、不能正常显示在表格中。

处理方法:

在定义mykgl时。对于该类的属性没有定义get set .定义后问题解决。

2、可以删除添加,但是不能手动修改DataGridView中的内容。

解决方法:

public class mykgl : INotifyPropertyChanged 在定义类时 继承了接口INotifyPropertyChanged后

问题解决。

3、对于绑定了List<T>具有泛型的数据的不能自动完成排序功能的处理

为了完成这个功能,就要引入两个自定义类。并且这两个类要实现规定的接口

既然具有排序功能,顾名思义肯定要实现的一个接口就是IComparer<T>

 

public class MyCompare<T> : System.Collections.Generic.IComparer<T>
     {
         private PropertyDescriptor property;
         private ListSortDirection direction;

        public MyCompare(PropertyDescriptor property, ListSortDirection direction)
         {
             this.property = property;
             this.direction = direction;
         }
         public int Compare(T x, T y)
         {
             object xValue = property.GetValue(x);//x.GetType().GetProperty(property.Name).GetValue(x, null);
             object yValue = property.GetValue(y); //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();
         }

   }


 

因为绑定的数据是一个列表,所以需要对绑定的列表的一些方法进行重写。所以引入了另一个类,这个类继承自BindingList<T>

由于 BindingList<T> 类未提供排序的基实现,因此默认情况下 ApplySortCore 始终引发 NotSupportedException

若要启用排序,请从 BindingList<T> 派生一个类并执行下列任务:

此外,您可能还需要实现附加的 SortDirectionCoreSortPropertyCore 排序属性

 

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

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

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

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

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

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

        protected override void RemoveSortCore()
         {
             //base.RemoveSortCore();
             isSorted = false;
             this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

        }

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

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

            sortProperty = prop;
             sortDirection = direction;
             this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

        }
     }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值