zoukankan      html  css  js  c++  java
  • DataGridView点击排序完成后如何禁止自动排序

    Summary: Disable sorting after clicking DataGridView columnheader,Prevent databound DataGridView from sorting while editing!

    Problem:I have a databound DataGridView in a WinForms which the user may have sorted by a column. The problem is this: after the user leaves a row after editing a cell in the sorted column, the row is immediately re-sorted.This is very disorienting for users and makes editing groups of rows together impossible.

    Solution: re-databound when user start editing, run program, or sorted finish, to do that can disable automatic re-sorting.ting after an initial sort and then only sort again when the user requests it.

    /// <summary>
    /// 控件状态 When the Program start or stop, we can re-databound dgv to disable re-sorting 
    /// </summary>
    public void ChangeStatus(bool status)
    {
        Invoke((ThreadStart)delegate()
        {                
            mydgv.Columns["status"].SortMode = status ? DataGridViewColumnSortMode.Automatic : DataGridViewColumnSortMode.NotSortable;//排序功能状态更改
            if (status)
            {
                mydgv.Sort(mydgv.Columns["status"], ListSortDirection.Ascending); //停止运行任务,自动排序
            }
            else
            {
                DataGridViewRebindsource(mydgv); //开始运行任务,排序完后重新绑定数据
            }
        });
    }
    
    /// <summary>
    /// 列头单击排序事件——after ColumnHeaderMouseClick to re-databound dgv to disable re-sorting
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void mydgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        switch (mydgv.SortOrder)
        {
            case System.Windows.Forms.SortOrder.None:
                mydgv.Sort(mydgv.Columns[e.ColumnIndex], ListSortDirection.Ascending);
                break;
            case System.Windows.Forms.SortOrder.Ascending:
                mydgv.Sort(mydgv.Columns[e.ColumnIndex], ListSortDirection.Descending);
                break;
            case System.Windows.Forms.SortOrder.Descending:
                mydgv.Sort(mydgv.Columns[e.ColumnIndex], ListSortDirection.Ascending);
                break;
        }
    
        DataGridViewRebindsource(mydgv); //排序完后重新绑定数据
    }
    
    /// <summary>
    /// 重新绑定dgv数据 清除自动排序 —— function: re-databound dgv to disable re-sorting 
    /// </summary>
    /// <param name="dgv"></param>
    public void DataGridViewRebindsource(DataGridView dgv)
    {
        DataTable table = new DataTable();
        int dgvColumnsNum = dgv.Columns.Count;
        int dgvRowsNum = dgv.Rows.Count;
        //获取datagridview的列
        foreach (DataGridViewColumn dgvc in dgv.Columns)
        {
            if (dgvc.Visible && dgvc.CellType != typeof(DataGridViewCheckBoxCell))
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = dgvc.DataPropertyName;
                table.Columns.Add(dc);
            }
        }
        //获取datagridview的行
        for (int i = 0; i < dgvRowsNum; i++)
        {
            DataRow dataRow = table.NewRow();
            int j = 0;
            for (j = 0; j < dgvColumnsNum; j++)
            {
                dataRow[j] = dgv.Rows[i].Cells[j].Value;
            }
            table.Rows.Add(dataRow);
        }
        dgv.DataSource = table;
    }
    牧羊童Gamir——随遇而安,保持一颗愉快之心!
  • 相关阅读:
    Spring学习总结之高级装配
    Spring学习总结之装配bean
    NS2安装过程中环境变量设置的问题(ns-2.35)
    =======================分割线======================================
    java的内存管理机制
    Python之面向对象编程
    Python之列表生成式
    Python之函数的参数
    Git基础级介绍
    第四次作业——个人作业——软件案例分析
  • 原文地址:https://www.cnblogs.com/gamir/p/4481561.html
Copyright © 2011-2022 走看看