不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
DataGridView控件必须绑定到IBindingList对象才能排序解决方法
编辑:dnawo 日期:2015-01-23
DataGridView使用List<T>做数据源:
调用Sort方法排序出错,提示:
我们知道,使用DataTable做数据源时排序是没问题的,所以首先想到的是有没有办法将List<T>转成DataTable?有网友提供了两个扩展方法经测试可行:
另一个思路是根据错误信息实现自己的IBindingList对象,也有网友提供了方法:
相关资料
[1].List<T>转换为DataTable:http://blog.csdn.net/emoonight/article/details/6617683
[2].DataGridView绑定BindingList<T>带数据排序的类:http://www.cnblogs.com/zfanlong1314/p/3573371.html
复制内容到剪贴板 程序代码
List<Person> people = new List<Person>();
//todo
dataGridView1.DataSource = people;
//todo
dataGridView1.DataSource = people;
调用Sort方法排序出错,提示:
引用内容
DataGridView 控件必须绑定到 IBindingList 对象才能排序
我们知道,使用DataTable做数据源时排序是没问题的,所以首先想到的是有没有办法将List<T>转成DataTable?有网友提供了两个扩展方法经测试可行:
复制内容到剪贴板 程序代码
public static class DataTableExtensions
{
/// <summary>
/// List<T>转DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this List<T> list)
{
DataTable result = new DataTable();
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(T);
Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { pList.Add(prop); result.Columns.Add(prop.Name, prop.PropertyType); });
foreach (var item in list)
{
DataRow row = result.NewRow();
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
result.Rows.Add(row);
}
return result;
}
/// <summary>
/// DataTable转List<T>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static List<T> ToList<T>(this DataTable table) where T : class, new()
{
List<T> result = new List<T>();
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(T);
Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { if (table.Columns.IndexOf(prop.Name) != -1) pList.Add(prop); });
foreach (DataRow row in table.Rows)
{
T obj = new T();
pList.ForEach(prop => { if (row[prop.Name] != DBNull.Value) prop.SetValue(obj, row[prop.Name], null); });
result.Add(obj);
}
return result;
}
}
{
/// <summary>
/// List<T>转DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this List<T> list)
{
DataTable result = new DataTable();
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(T);
Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { pList.Add(prop); result.Columns.Add(prop.Name, prop.PropertyType); });
foreach (var item in list)
{
DataRow row = result.NewRow();
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
result.Rows.Add(row);
}
return result;
}
/// <summary>
/// DataTable转List<T>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static List<T> ToList<T>(this DataTable table) where T : class, new()
{
List<T> result = new List<T>();
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(T);
Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { if (table.Columns.IndexOf(prop.Name) != -1) pList.Add(prop); });
foreach (DataRow row in table.Rows)
{
T obj = new T();
pList.ForEach(prop => { if (row[prop.Name] != DBNull.Value) prop.SetValue(obj, row[prop.Name], null); });
result.Add(obj);
}
return result;
}
}
复制内容到剪贴板 程序代码
dataGridView1.DataSource = people.ToDataTable();
另一个思路是根据错误信息实现自己的IBindingList对象,也有网友提供了方法:
复制内容到剪贴板 程序代码
/// <summary>
/// 提供支持数据绑定的泛型集合
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindingCollection<T> : BindingList<T>
{
private bool _isSortedCore = true;
private ListSortDirection _sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor _sortPropertyCore = null;
/// <summary>
/// 是否已排序
/// </summary>
protected override bool IsSortedCore
{
get { return _isSortedCore; }
}
/// <summary>
/// 获取列表的排序方向
/// </summary>
protected override ListSortDirection SortDirectionCore
{
get { return _sortDirectionCore; }
}
/// <summary>
/// 获取用于对列表排序的属性说明符
/// </summary>
protected override PropertyDescriptor SortPropertyCore
{
get { return _sortPropertyCore; }
}
/// <summary>
/// 是否支持排序
/// </summary>
protected override bool SupportsSortingCore
{
get { return true; }
}
/// <summary>
/// 是否支持搜索
/// </summary>
protected override bool SupportsSearchingCore
{
get { return true; }
}
/// <summary>
/// 构造函数
/// </summary>
public BindingCollection()
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list"></param>
public BindingCollection(IList<T> list)
: base(list)
{ }
/// <summary>
/// 对项排序
/// </summary>
/// <param name="property"></param>
/// <param name="direction"></param>
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);
_isSortedCore = true;
_sortDirectionCore = direction;
_sortPropertyCore = property;
}
else
{
_isSortedCore = false;
}
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
/// <summary>
/// 移除排序
/// </summary>
protected override void RemoveSortCore()
{
_isSortedCore = false;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
public class ObjectPropertyCompare<T> : IComparer<T>
{
/// <summary>
/// 属性
/// </summary>
public PropertyDescriptor Property { get; set; }
/// <summary>
/// 排序方向
/// </summary>
public ListSortDirection Direction { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public ObjectPropertyCompare()
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="property"></param>
/// <param name="direction"></param>
public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
{
Property = property;
Direction = direction;
}
/// <summary>
/// 比较两个对象
/// </summary>
/// <param name="x"></param>
/// <param name="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 == null && yValue == null)
{
returnValue = 0;
}
else if (xValue == null)
{
returnValue = -1;
}
else if (yValue == null)
{
returnValue = 1;
}
else 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;
}
}
}
/// 提供支持数据绑定的泛型集合
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindingCollection<T> : BindingList<T>
{
private bool _isSortedCore = true;
private ListSortDirection _sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor _sortPropertyCore = null;
/// <summary>
/// 是否已排序
/// </summary>
protected override bool IsSortedCore
{
get { return _isSortedCore; }
}
/// <summary>
/// 获取列表的排序方向
/// </summary>
protected override ListSortDirection SortDirectionCore
{
get { return _sortDirectionCore; }
}
/// <summary>
/// 获取用于对列表排序的属性说明符
/// </summary>
protected override PropertyDescriptor SortPropertyCore
{
get { return _sortPropertyCore; }
}
/// <summary>
/// 是否支持排序
/// </summary>
protected override bool SupportsSortingCore
{
get { return true; }
}
/// <summary>
/// 是否支持搜索
/// </summary>
protected override bool SupportsSearchingCore
{
get { return true; }
}
/// <summary>
/// 构造函数
/// </summary>
public BindingCollection()
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list"></param>
public BindingCollection(IList<T> list)
: base(list)
{ }
/// <summary>
/// 对项排序
/// </summary>
/// <param name="property"></param>
/// <param name="direction"></param>
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);
_isSortedCore = true;
_sortDirectionCore = direction;
_sortPropertyCore = property;
}
else
{
_isSortedCore = false;
}
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
/// <summary>
/// 移除排序
/// </summary>
protected override void RemoveSortCore()
{
_isSortedCore = false;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
public class ObjectPropertyCompare<T> : IComparer<T>
{
/// <summary>
/// 属性
/// </summary>
public PropertyDescriptor Property { get; set; }
/// <summary>
/// 排序方向
/// </summary>
public ListSortDirection Direction { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public ObjectPropertyCompare()
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="property"></param>
/// <param name="direction"></param>
public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
{
Property = property;
Direction = direction;
}
/// <summary>
/// 比较两个对象
/// </summary>
/// <param name="x"></param>
/// <param name="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 == null && yValue == null)
{
returnValue = 0;
}
else if (xValue == null)
{
returnValue = -1;
}
else if (yValue == null)
{
returnValue = 1;
}
else 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;
}
}
}
复制内容到剪贴板 程序代码
dataGridView1.DataSource = new BindingCollection<Person>(people);
相关资料
[1].List<T>转换为DataTable:http://blog.csdn.net/emoonight/article/details/6617683
[2].DataGridView绑定BindingList<T>带数据排序的类:http://www.cnblogs.com/zfanlong1314/p/3573371.html
上一篇: JavascriptSerializer序列化/反序列化DateTime少了8小时原因分析
下一篇: 网络牛肉面馆网址
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
下一篇: 网络牛肉面馆网址
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 6864
发表评论
请登录后再发表评论!