我正在尝试对数据网格视图的总行进行排序,该行是来自数据表的数据源。我想要实现的是每次用户单击datagrid列时都将总行排除在排序之外。我正在使用DataGridViewCellMouseEventArgs捕获此事件。这是我的代码:
private void dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridView dgv = sender as DataGridView;
var columnSortDirection = string.Empty;
var columnName = dgv.SortedColumn.DataPropertyName;
if (dgv.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection == SortOrder.Descending)
{
columnSortDirection = "DESC";
}
else
{
columnSortDirection = "ASC";
}
//remove total row before sort
var lastRow = dt.Rows[dt.Rows.Count - 1];
dt.Rows.Remove(lastRow);
//sort datatable
DataView dv = dt.DefaultView;
dv.Sort = string.Format("{0} {1}", columnName, columnSortDirection);
DataTable sortedDt = dv.ToTable();
//reinserting total row after dt is sorted
InsertTotalRow(sortedDt);
dgv.DataSource = sortedDt;
}
该代码仅适用于升序,而不适用于其他代码。无论出于何种原因,当用户再次单击列时,它都永远不会将方向更改为下降。我在这里做什么错了?