list集合绑定在datagridview上时如何实现排序

摘要:
objList=newBindingCollection<人>DataGridViewTextBoxColumnGridView01Name=newDataGridViewTextBoxColumn();});dgv1.DataSource=lst;this.direction=方向;公共比较(Tx;

List<Person> lst = new List<Person>();
lst.Add(new Person("A", "1"));
lst.Add(new Person("C", "2"));
lst.Add(new Person("B", "3"));


BindingCollection<Person> objList = new BindingCollection<Person>();
//加载数据
foreach (Person item in lst)
{
objList.Add(item);
}

DataGridViewTextBoxColumn GridView01NO=new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn GridView01Name=new DataGridViewTextBoxColumn();

GridView01NO.HeaderText = "编号";
GridView01NO.Name = "strNo";
GridView01NO.DataPropertyName = "strNo";

GridView01Name.HeaderText = "姓名";
GridView01Name.Name = "strName";
GridView01Name.DataPropertyName = "strName";

dgv1.Columns.AddRange(new DataGridViewColumn[] {GridView01NO,GridView01Name, });
//dgv1.DataSource = objList;
dgv1.DataSource = lst;

datagridview的数据源是list集合时,是不能排序的,不过调用了BindingCollection类后就可以了,

public 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
}


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

免责声明:文章转载自《list集合绑定在datagridview上时如何实现排序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于 mybatis 中 in 写法,&amp;lt;foreach collection="xxx" item="xxx" index="index" open="(" separator="," close=")"&amp;gt; 参数详解55个美丽而独特的网站页眉设计欣赏下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Avue的CRUD最强封装(三)

目录 CRUD最强封装-极简增删改查 1、封装crud.js 2、在@/views/modules/下新建.vue页面 3、live template 模板 CRUD最强封装-极简增删改查 由于@/api/x-api.js和@/option/xx-option.js和@/views/modules/xx-vue.js中有大量的重复代码,因此我...

高德地图API之货车路线

货车路线: 引入 AMap.TruckDriving 注意:和驾车路线、步行路线不同的是,必须指定cidy和size,并且坐标传入为json格式 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title&g...

【Vue入门】利用VueCli搭建基本框架--在Home页实现上左右基本布局(五)

上一节讲了简单封装Http请求并调用登陆的Api接口 这节主要说Home的布局展示 一、设置布局代码   Home页布局参考Elementui中的布局代码(采用上,左右结构)  当然也可以用其他的布局 <el-container> <el-header>Header</el-header> <el-cont...

vue中选择图片,预览图片,返回base64

1.html内容 <el-col :span="20" style="margin-left: 100px;"> <input type="file" name="avatar" accept="image/gif,image/jpeg,image/jpg,image/png" @change="changeCoverImg($ev...

svg的stroke属性,神奇的描边

1、stroke 定义一条线,文本或元素轮廓颜色 2、stroke-width 定义一条线,文本或元素轮廓厚度 3、stroke-linecap 描边端点表现形式 <svg> <g fill='none' stroke='black' stroke-width='10'> <path stroke-linecap...

搜索插件——autocomplete

搜索插件的功能是通过插件的autocomplete()方法与文本框相绑定,当文本框输入字符时,绑定后的插件将返回与字符相近的字符串提示选择,调用格式如下: $(textbox).autocomplete(urlData,[options]); 其中,textbox参数为文本框元素名称,urlData为插件返回的相近字符串数据,可选项参数options为调用...