DataGridView不能正常显示、编辑、排序的处理
2014-03-27 11:28
351 查看
BindingList<mykgl> kgllist 这是要绑定的的list
其中mykgl是自定义的一个类。
把kgllist设置为DataGridView的DataSource后。
1、不能正常显示在表格中。
处理方法:
在定义mykgl时。对于该类的属性没有定义get set .定义后问题解决。
2、可以删除添加,但是不能手动修改DataGridView中的内容。
解决方法:
public class mykgl : INotifyPropertyChanged 在定义类时 继承了接口INotifyPropertyChanged后
问题解决。
3、对于绑定了List<T>具有泛型的数据的不能自动完成排序功能的处理
为了完成这个功能,就要引入两个自定义类。并且这两个类要实现规定的接口
既然具有排序功能,顾名思义肯定要实现的一个接口就是IComparer<T>
因为绑定的数据是一个列表,所以需要对绑定的列表的一些方法进行重写。所以引入了另一个类,这个类继承自BindingList<T>
由于 BindingList<T> 类未提供排序的基实现,因此默认情况下
ApplySortCore 始终引发 NotSupportedException。
若要启用排序,请从 BindingList<T> 派生一个类并执行下列任务:
重写 ApplySortCore 并实现排序,使得在排序完成时引发
ListChanged 事件。
重写 RemoveSortCore 并实现排序移除。
重写 SupportsSortingCore 并将
SupportsSortingCore 设置为
true。
此外,您可能还需要实现附加的 SortDirectionCore 和
SortPropertyCore 排序属性
其中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> 派生一个类并执行下列任务:
重写 ApplySortCore 并实现排序,使得在排序完成时引发
ListChanged 事件。
重写 RemoveSortCore 并实现排序移除。
重写 SupportsSortingCore 并将
SupportsSortingCore 设置为
true。
此外,您可能还需要实现附加的 SortDirectionCore 和
SortPropertyCore 排序属性
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 = a62f new MyCompare<T>(prop, direction); items.Sort(pc); isSorted = true; } else { isSorted = false; } sortProperty = prop; sortDirection = direction; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } }
相关文章推荐
- Jira中Activity Stream中显示Localhost不能正常访问的处理
- MATLAB读取nc文件写成TIF文件arcgis统计错误 -不能正常显示统计信息,空值处理
- ckplayer播放器设置默认封面时部分浏览器不能正常显示封面图片问题的处理
- 解决 qtcreator编辑*.ui文件时右键菜单不能正常显示但的问题
- R编辑环境中不能正常显示英语的解决办法
- 在.net 环境下,进行了伪静态页面处理后,后台的Fckeditor就不能正常显示了
- R编辑环境中不能正常显示英语的解决办法
- .net 4.0发布后不能正常显示图片问题,将iis应用程序池设置成4.0版本,报告错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- 完整处理Opera图标错误 不能正常显示的问题
- 解决 qtcreator编辑*.ui文件时右键菜单不能正常显示但的问题
- 关于java中汉字不能正常显示的处理方案
- ubuntu上pdf文档不能正常显示处理方法
- 关于ewebeditor编辑时不能正常显示解决
- 解决 qtcreator编辑*.ui文件时右键菜单不能正常显示但的问题
- 图片路径中含有中文在jsp下不能正常显示的原因及解决
- Datagridview的排序和CellEndEdit事件问题处理
- Windows 7部分程序图标不能正常显示的解决方法
- emoji 表情不能存入mysql 数据库和在jsp 页面显示的处理
- 托盘在XP下不能显示tooltip,在Vista和Windows7下正常
- fckeditor上传的小窗口在IE9下不能正常显示的问题解决办法