您的位置:首页 > 其它

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>

 

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

}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐