List 在c中对绑定到类列表的dataGridView进行排序的优雅方法#
我的问题是找到一种方法来动态排序dataGridView,其中datasource绑定到对象列表,并以如下方式调用它:List 在c中对绑定到类列表的dataGridView进行排序的优雅方法#,list,sorting,compare,List,Sorting,Compare,我的问题是找到一种方法来动态排序dataGridView,其中datasource绑定到对象列表,并以如下方式调用它: private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { myList.Sort((x,y) => x.getfield(e.ColumnIndex).CompareTo(y.getfield(e.Colu
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
myList.Sort((x,y) => x.getfield(e.ColumnIndex).CompareTo(y.getfield(e.ColumnIndex)))) ;
}
我解释我的情况。
我的班级是这样定义的:
public class FileWorking
{
private string Host;
private string Id;
private string Path;
private string Filename;
private string Username;
private string ModRW;
private DateTime Starttime;
private DateTime Endtime;
private int Status;
public string host { get => Host; set => Host = value; }
public string id { get => Id; set => Id = value; }
public string path { get => Path; set => Path = value; }
public string filename { get => Filename; set => Filename = value; }
public string username { get => Username; set => Username = value; }
public string modRW { get => ModRW; set => ModRW = value; }
public DateTime starttime { get => Starttime; set => Starttime = value; }
public DateTime endtime { get => Endtime; set => Endtime = value; }
public int stato {get => Stato; set => Stato=value;}
public TimeSpan active { get => (Endtime - Starttime); }
}
然后列出:
public List<FileWorking> historyFiles;
现在,我想对单击的每列的dataGridView1进行排序。
我是这样做的(因为dataGridView1.Sort(e.ColumnIndex)通过一个异常):
我知道这不是最好的解决方案:
-比较用于每个字段,即使类型相似
-如果我添加一些其他属性,我会在开关中添加特定的行
我尝试在FileWorking类中实现此函数:
public object getfield(int fieldindex)
{
Type t = this.GetType();
PropertyInfo[] props = t.GetProperties();
object prop =props.GetValue(fieldindex);
return prop;
}
但当我尝试这样调用时,我无法编译(实际上,对象没有CompareTo,即使int、string、DateTime、TimeSpan是……):
有什么办法吗
提前感谢,,
Filippo根据这个答案,以这种方式解决:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
var propInfo = typeof(FileWorking).GetProperty(dataGridView1.Columns[e.ColumnIndex].Name);
Comparison<FileWorking> asc = (t1, t2) => ((IComparable)propInfo.GetValue(t1, null)).CompareTo(propInfo.GetValue(t2, null));
historyFiles.Sort(asc);
}
private void dataGridView1\u ColumnHeaderMouseClick(对象发送者,DataGridViewCellMouseEventArgs e)
{
var propInfo=typeof(FileWorking).GetProperty(dataGridView1.Columns[e.ColumnIndex].Name);
比较asc=(t1,t2)=>((i可比较)propInfo.GetValue(t1,null)).CompareTo(propInfo.GetValue(t2,null));
历史文件排序(asc);
}
public object getfield(int fieldindex)
{
Type t = this.GetType();
PropertyInfo[] props = t.GetProperties();
object prop =props.GetValue(fieldindex);
return prop;
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
historyFiles.Sort((x,y) => x.getfield(e.ColumnIndex).CompareTo(y.getfield(e.ColumnIndex)))) ;
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
var propInfo = typeof(FileWorking).GetProperty(dataGridView1.Columns[e.ColumnIndex].Name);
Comparison<FileWorking> asc = (t1, t2) => ((IComparable)propInfo.GetValue(t1, null)).CompareTo(propInfo.GetValue(t2, null));
historyFiles.Sort(asc);
}