本来用DataGridView.DataSource绑定的是可排序 SortableBindingList
但是无法实现点击多个列排序,要想实现还是有点复杂.
最终实现的效果: 按住shift键点击列标题,按照点击的先后顺序依次排序,效果等同于OrderBy.ThenBy.ThenBy.ThenBy.ThenBy......
由于 SortableBindingList 本身实现了点击单列自动排序,
第一步、就是禁用自动排序功能
如果绑定的是 List<T> 就跳过这一步
我用了一个checkbox来控制,本来打算用按下shift来实现,但不够保险.
checkBox_SortMult.CheckedChanged += (s, e) =>
{
BeginInvoke(new Action(() =>
{
SortMult = checkBox_SortMult.Checked;
DataGridViewColumnSortMode SortMode = SortMult ? DataGridViewColumnSortMode.NotSortable : DataGridViewColumnSortMode.Automatic;
dataGridView_items.Columns.Cast<DataGridViewColumn>().ToList().ForEach(c => c.SortMode = SortMode);
}));
};
第二步、注册各种事件,我选择了按下和放开shift键作为多列排序的起始标记。
List<DataGridViewColumn> SelectedSortColumns_Items = new List<DataGridViewColumn>();//多列排序列
bool ItemsShift = false;
private void SetEvents_items()
{
dataGridView_items.KeyDown += (s, e) =>
{
switch (e.KeyCode)
{
case Keys.ShiftKey:
if (!ItemsShift)
{
SelectedSortColumns_Items.Clear();
ItemsShift = true;
}
break;
}
};
dataGridView_items.KeyUp += (s, e) =>
{
switch (e.KeyCode)
{
case Keys.ShiftKey:
SelectedSortColumns_Items.Clear();
ItemsShift = false;
break;
}
};
//注册鼠标点击列标题事件,这里传入 SelectedSortColumns_Items, ItemsShift 是为了兼容多个DGV控件,为了最大程度的保险,每个DGV最好建立一个对应的 SelectedSortColumns_Items, ItemsShift 变量
dataGridView_items.ColumnHeaderMouseClick += (s, e) => { DataGridView_MultSort(s, e, SelectedSortColumns_Items, ItemsShift); };
}
第三布、列标题点击事件处理
private void DataGridView_MultSort(object sender, DataGridViewCellMouseEventArgs e, List<DataGridViewColumn> SelectedSortColumns, bool IsShift)
{
if (e.Button != MouseButtons.Left)
return;
if (!SortMult)
return;
if (!IsShift && MessageBox.Show("请按住 Shift 再按顺序点击") == DialogResult.OK)
return;
var dv = (DataGridView)sender;
dv.Columns[e.ColumnIndex].Selected = true;
if (!SelectedSortColumns.Contains(dv.Columns[e.ColumnIndex]))
SelectedSortColumns.Add(dv.Columns[e.ColumnIndex]);
DataGridView_MultSortByColumns(dv, SelectedSortColumns);
}
private void DataGridView_MultSortByColumns(DataGridView dv, List<DataGridViewColumn> SelectedSortColumns)
{
var SortPropetyNames = SelectedSortColumns.Select(c => c.DataPropertyName).ToDictionary(k => k, v => (Func<string, object, object>)null);
if (dv == dataGridView_items)
{
var datasoure = (SortableBindingList<这里是你的实体类Class>)dataGridView_items.DataSource;
//这里之所以用SortableBindingList.SortMult,而不是用传统的 OrderBy.ThenBy 是因为不需要重新绑定数据,如果你的 datagridview 有复杂的宽高设置和列标题设置等等,用重新绑定的方式不是理想的选择
datasoure.SortMult(SortPropetyNames);
}
}
第四步、SortableBindingList 类,dataGridView.DataSource 绑定必须用这个。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Extension.Sys;
namespace Extension.Forms
{
/// <summary>
/// 可排序 BindingList
/// </summary>
/// <typeparam name="T"></typeparam>
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();
}
#region ======== 多重排序 ========
Dictionary<string, Func<string, object, object>> SortPropetyNames;
//var SortPropetyNames = SelectedSortColumns.Select(c => c.DataPropertyName).ToDictionary(k => k, v => (Func<string, object, object>)null);
/// <summary>
/// 具体示例点进来看注释.如果嫌 _SortPropetyNames 参数过于复杂,可以将它的类型改为List<string> , CompareCore_Mult里面也稍微改下即可
/// </summary>
/// <param name="_SortPropetyNames">key 是属性名称,用来取属性值, value 是对属性排序前的处理,留 null 则直接比较值本身.
/// Func《string, object, object》 参数解析:
/// 第一个参数 string 为属性名,因为有可能需要通过属性名进行一些判断,
/// 第二个 object 是属性值,具体类型要写的时候强转,
/// 最后 object 是返回排序的数据</param>
public void SortMult(Dictionary<string, Func<string, object, object>> _SortPropetyNames)
{
SortPropetyNames = _SortPropetyNames;
sortDirectionCore = ListSortDirection.Ascending;
List<T> list = (this.Items as List<T>);
list.Sort(CompareCore_Mult);
ResetBindings();
}
private int CompareCore_Mult(T o1, T o2)
{
int ret = 0;
foreach(var d in SortPropetyNames)
{
if (ret == 0)
{
var SortPropetyName = d.Key;
var SortHandler = d.Value;
var SortPropertyValueType = o1.GetPropertyValueType(SortPropetyName);
var o1Value = o1.GetValueByName(SortPropetyName);
var o2Value = o2.GetValueByName(SortPropetyName);
ret = SortHandler == null
? CompareValue(o1Value, o2Value, o1.GetPropertyValueType(SortPropetyName))
: CompareValue(SortHandler(SortPropetyName, o1Value), SortHandler(SortPropetyName, o2Value), SortPropertyValueType);
}
}
if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
return ret;
}
#endregion
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)
{
//MessageBox.Show(type.GetType().ToString());
if (o1 == null) return o2 == null ? 0 : -1;
if (o2 == null) return 1;
if (type.IsValueType) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));//注意这里if (type.IsPrimitive || type.IsEnum)是取不到Int型的
if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}
}
这里面用到了一个反射取值的扩展方法
/// <summary>
/// 获取属性或字段值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="isPropertyType">如果 false 就取 GetFields </param>
/// <returns></returns>
public static object GetValueByName<T>(this T obj, string propertyName, bool isPropertyType = true)
{
try
{
Type t = obj.GetType();
if (isPropertyType)
{
foreach (PropertyInfo pi in t.GetProperties())
{
var name = pi.Name;
if (name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))
{
var value = pi.GetValue(obj, null);
return value;
}
}
}
else
{
foreach (FieldInfo pi in t.GetFields())
{
var name = pi.Name;
if (name.Equals(propertyName,StringComparison.OrdinalIgnoreCase))
{
var value = pi.GetValue(obj);
return value;
}
}
}
return string.Empty;
}
catch { return string.Empty; }
}
效果:
原始排序:
排序后: