我有一个包含3列的datagridview。但是我没有将数据源绑定到datagridview。 它具有三列。
I have a datagridview that contain 3 columns. But I hv not bind a datasource to datagridview. It has three columns.
我可以在datagridview中编辑EmpName。
I can edit EmpName in datagridview.
我想在编辑一行后对datagridview中的内容进行排序。
I want to sort the content in the datagridview after i edit a row.
我想首先按EmpName排序,然后按InTime排序。 时间采用AM,PM时间格式(例如:2:00 PM)。
I want to sort first by EmpName and then by InTime. The time is in AM , PM Time format (Ex: 2:00 PM).
我只能按一列对数据进行排序。 我已经使用过,
I can sort data by only one column. I have used,
dgvSchedule.Sort(dgvSchedule.Columns[0],ListSortDirection.Ascending);但是如何按多列排序。特别是当时间采用AM PM格式时。 请帮助我。
But how to sort by multiple columns. Specially when the time is in AM PM format. Please help me.
谢谢。
推荐答案以某种方式实现 IComparer :
private class CustomComparer : IComparer { private static int SortOrder = 1; public CustomComparer(SortOrder sortOrder) { SortOrder = sortOrder == SortOrder.Ascending ? 1 : -1; } public int Compare(object x, object y) { DataGridViewRow row1 = (DataGridViewRow)x; DataGridViewRow row2 = (DataGridViewRow)y; int result = row1.Cells["EmpName"].Value.ToString().CompareTo( row2.Cells["EmpName"].Value.ToString()); if ( result == 0 ) { result = DateTime.ParseExact( row1.Cells["InTime"].Value.ToString(), "h:mm tt", CultureInfo.InvariantCulture).TimeOfDay .CompareTo( DateTime.ParseExact( row2.Cells["InTime"].Value.ToString(), "h:mm tt", CultureInfo.InvariantCulture).TimeOfDay); } return result * SortOrder; }用法是:
dgvSchedule.Sort(new CustomComparer(ListSortDirection.Ascending));更多推荐
按两列对Datagridview进行排序
发布评论