DataGridView实现点击列头升序和降序排序
private void dgv_Users_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[e.ColumnIndex].SortMode == DataGridViewColumnSortMode.Programmatic)
{
string columnBindingName = dgv.Columns[e.ColumnIndex].DataPropertyName;
switch (dgv.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection)
{
case System.Windows.Forms.SortOrder.None:
case System.Windows.Forms.SortOrder.Ascending:
CustomSort(columnBindingName, "desc");
dgv.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Descending;
break;
case System.Windows.Forms.SortOrder.Descending:
CustomSort(columnBindingName, "asc");
dgv.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Ascending;
break;
}
}
}
///
/// 自定义排序
///
/// 绑定的字段名
/// 排序方式 asc 升序 desc 降序
private void CustomSort(string columnBindingName, string sortMode)
{
DataTable dt = this.dgv_Users.DataSource as DataTable;
DataView dv = dt.DefaultView;
dv.Sort = columnBindingName + " " + sortMode;
this.dgv_Users.DataSource = dv.ToTable();
this.dgv_Users.Refresh();
}
相关文章
- C# OleDbDataReader快速数据读取方式(3种)
- C# DataGridView行列转换的具体实现
- WinForm中DataGridView添加,删除,修改操作具体方法
- SQLite Database Browser数据库查看器图文使用教程
- data interface天翼无线网卡驱动程序
- DataGridView实现点击列头升序和降序排序
- asp.net中IDataParameter调用存储过程的实现方法
- appdata文件夹有什么用途?C盘appdata可以删除吗?
- ASP.NET数据绑定之DataList控件实战篇
- autonavidata50是什么文件夹 autonavidata50文件夹可以删除吗