DataGridView的绑定列排序问题

txt_ly 2007-08-30 02:08:05
DataGridView是通过DataSource绑定的,
DataGridView里隐藏列,显示列
设置列userName为自动排序
dgv.Columns["userName"].SortMode =DataGridViewColumnSortMode.Automatic;
怎么点击列标题就不起作用
...全文
1748 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangs_1 2011-07-21
  • 打赏
  • 举报
回复
看了上面的代码,不知道怎么用,谁能说一下
yzzhouyuefeng511 2011-06-13
  • 打赏
  • 举报
回复
同谢同谢
yongfeiabc 2008-08-05
  • 打赏
  • 举报
回复
同谢!
bb_chen 2007-11-07
  • 打赏
  • 举报
回复
哈哈,終於被我找到了。我也是這個問題 找了好久,都沒解決。謝謝
hancat 2007-09-19
  • 打赏
  • 举报
回复
感谢danjiewu(阿丹)终于帮我解决了难题!
danjiewu 2007-09-15
  • 打赏
  • 举报
回复
public class SortableBindingList<T> : BindingList<T>
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem;

public SortableBindingList() : base() { }

public SortableBindingList(IList<T> list) : base(list) { }

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

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

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

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

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

protected override int FindCore(PropertyDescriptor prop, object key)
{
for (int i = 0; i < this.Count; i++)
{
if (Equals(prop.GetValue(this[i]), key)) return i;
}
return -1;
}

protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = prop;
sortDirectionCore = direction;
Sort();
}

protected override void RemoveSortCore()
{
if (isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
Sort();
}
}

public string DefaultSortItem
{
get { return defaultSortItem; }
set
{
if (defaultSortItem != value)
{
defaultSortItem = value;
Sort();
}
}
}

private void Sort()
{
List<T> list = (this.Items as List<T>);
list.Sort(CompareCore);
ResetBindings();
}

private int CompareCore(T o1, T o2)
{
int ret = 0;
if (SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
if (property != null)
{
ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
}
}
if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
return ret;
}

private static int CompareValue(object o1, object o2, Type type)
{
//这里改成自己定义的比较
if(o1 == null)return o2==null ? 0 : -1;
else if(o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}

---------------------
这个是自己实现的一个例子,可以根据需要修改一下。
panda83 2007-09-15
  • 打赏
  • 举报
回复
我也遇到这问题,楼上的高手能写详细点,写些如何绑定实现排序功能吗?
danjiewu 2007-08-31
  • 打赏
  • 举报
回复
自己继承BindingList<T>,不要绑定List<T>。
重写
protected virtual void ApplySortCore(PropertyDescriptor property, ListSortDirection direction);
protected virtual void RemoveSortCore();

// Core sort properties
protected virtual bool SupportsSortingCore { get; }
protected virtual bool IsSortedCore { get; }
protected virtual ListSortDirection SortDirectionCore { get; }
protected virtual PropertyDescriptor SortPropertyCore { get; }
这些方法,例子里已经写的很清楚了。
txt_ly 2007-08-31
  • 打赏
  • 举报
回复
哪位大佬能给个解决方案,我的数据源是泛型集合类为List<>
txt_ly 2007-08-31
  • 打赏
  • 举报
回复
DataGridView 控件必须绑定到 IBindingList 对象才能排序。
但我用数据源是泛型集合类,没有实现IBindingList接口,怎么办
danjiewu 2007-08-31
  • 打赏
  • 举报
回复
http://www.microsoft.com/taiwan/msdn/library/2005/Mar-2005/winforms02182005.htm
这个应该是你要的。
danjiewu 2007-08-31
  • 打赏
  • 举报
回复
搜索下BindingList<T>
danjiewu 2007-08-31
  • 打赏
  • 举报
回复
自己写一个SortableBindingList<T>:BindingList<T>,把需要排序的List<T>改成用SortableBindingList<T>,不明白?
txt_ly 2007-08-31
  • 打赏
  • 举报
回复
我现在的数据集采用的都是List<T>,照楼上这样改,不太现实啊
txt_ly 2007-08-30
  • 打赏
  • 举报
回复
同一个DataGridView,我用DataTable做为数据源时就可以自动排序!!~~
why??
txt_ly 2007-08-30
  • 打赏
  • 举报
回复
我用的数据源是泛型集合类
txt_ly 2007-08-30
  • 打赏
  • 举报
回复
楼上,能不能说清楚一点,我问的是winform 中的DataGridView
hu0516 2007-08-30
  • 打赏
  • 举报
回复
AllowSorting="True"

dgData_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧