zoukankan      html  css  js  c++  java
  • DataGridView的自定义列排序

    1,将需要进行排序的列做属性的设置

    this.colUserName.SortMode = DataGridViewColumnSortMode.Programmatic;

    2,添加列的事件

    //点击列头进行排序
    private void dgv_NoSignalSelect_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
    int nColumnIndex = e.ColumnIndex;

    if (dgv_NoSignalSelect.Columns[nColumnIndex].SortMode != DataGridViewColumnSortMode.Programmatic)
    {
    return;
    }
    switch (dgv_NoSignalSelect.Columns[nColumnIndex].HeaderCell.SortGlyphDirection)
    {
    case SortOrder.None:
    case SortOrder.Ascending:
    //在这里加入排序的逻辑
    OrderTableOgDgv(nColumnIndex, "asc");
    //设置列标题的状体
    dgv_NoSignalSelect.Columns[nColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Descending;
    break;
    default:
    //在这里加入排序的逻辑
    OrderTableOgDgv(nColumnIndex, "desc");
    //设置列标题的状体
    dgv_NoSignalSelect.Columns[nColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
    break;
    }


    }

    3,下面是上面应用到的排序算法

    /// <summary>
    /// 给dgv_NoSignalSelect按列排序
    /// </summary>
    /// <param name="nColumnIndex"></param>
    /// <param name="orderCmd"></param>
    private void OrderTableOgDgv(int nColumnIndex, string orderCmd)
    {
    DataTableEx dtex = this.dgv_NoSignalSelect.SourceDataTable;
    dtex = UserQueueServer.OrderTable(dtex, dgv_NoSignalSelect.Columns[nColumnIndex].Name, orderCmd);
    this.dgv_NoSignalSelect.SourceDataTable = dtex;
    this.dgv_NoSignalSelect.Refresh();
    }

    public Class UserQueueServer{

      /// <summary>
    /// 对DataTable排序
    /// </summary>
    /// <param name="dt">DataTableEx</param>
    /// <param name="columnname">排序的列名称</param>
    /// <param tyoe="type">排序类型 "desc"-降序 "asc"-升序</param>
    /// <returns></returns>
    public static DataTableEx OrderTable(DataTableEx dt, string columnname,string type)
    {
    DataTable ds = dt.Clone();
    ds.Clear();
    if (dt.Columns.Contains(columnname))
    {
    DataRow[] drs = dt.Select("1=1", columnname + " "+ type);
    foreach (DataRow dr in drs)
    {
    ds.Rows.Add(dr.ItemArray);
    }
    }
    return ds as DataTableEx;
    }

    }

  • 相关阅读:
    Nginx学习---Nginx的详解_【all】
    Nginx学习---企业级nginx环境搭建
    企业的批量分发的管理方案
    全网数据实时备份方案[inotify,sersync]
    搭建企业级全网数据定时备份方案[cron + rsync]2
    搭建企业级全网数据定时备份方案[cron + rsync]
    SSH连接不上
    ACC起来后,usb检测不到
    常用命令和配置文件位置
    Linux系统的信号详解
  • 原文地址:https://www.cnblogs.com/Robert-huge/p/5542599.html
Copyright © 2011-2022 走看看