【问题标题】:Prevent databound DataGridView from sorting while editing防止数据绑定 DataGridView 在编辑时排序
【发布时间】:2011-12-25 19:12:09
【问题描述】:

我在 Win Forms 应用程序中有一个数据绑定 DataGridView,用户可能已按列排序。问题是这样的:在用户编辑排好列的单元格后离开一行后,该行立即重新排序。

这让用户非常迷失方向,并且无法同时编辑多组行。

我正在寻找的解决方案将在初始排序后有效地禁用自动重新排序,然后仅在用户请求时再次排序。

【问题讨论】:

    标签: c# winforms sorting datagridview edit


    【解决方案1】:

    听起来您的 GridView 又是一次数据绑定。这意味着您的排序顺序将丢失。启用 gridview 的 Viewstate 并确保您没有在回发时绑定网格。

    【讨论】:

    • LOL 这个答案很有趣。我不会因此而否决它。 :)
    • 问题是关于winforms,而不是ASP web forms。
    【解决方案2】:

    为了他人的利益,这是我想出的解决方案,但我很想听听更好的解决方案。

    我在 DataTable 中添加了一个额外的非持久列,称为 SORT_ORDER,它仅用于排序。

    当用户单击要排序的列时,我将所选列中的值和值类型复制到 SORT_ORDER 列,然后按 SORT_ORDER 排序。由于 SORT_ORDER 不可见且无法编辑,因此即使用户编辑所选列,排序顺序也不会改变。事件处理程序如下所示:

        private void MyDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
            dirtyCellListenerEnabled = false;
    
            SORT_ORDER.ValueType = MyDataGridView.Columns[e.ColumnIndex].ValueType;
    
            foreach(DataGridViewRow r in MyDataGridView.Rows) {
                r.Cells[SORT_ORDER.Index].Value = r.Cells[e.ColumnIndex].Value;
            }
    
            switch(MyDataGridView.SortOrder) {
                case System.Windows.Forms.SortOrder.None:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending);
                    break;
                case System.Windows.Forms.SortOrder.Ascending:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Descending);
                    break;
                case System.Windows.Forms.SortOrder.Descending:
                    MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending);
                    break;
            }
            dirtyCellListenerEnabled = true;
        }
    

    请注意,我必须禁用并重新启用单元格侦听器,以便我的代码不会将排序列更新视为真正的更改。

    在找到此解决方案之前,我还尝试将排序列添加到 DataGridView,但它不起作用,因为 DataGridView 无法对其数据源中不存在的列进行排序。

    我确信我还可以进行其他一些调整,例如在填充 SORT_ORDER 并在所选列上显示排序字形时暂停更新。

    【讨论】:

    • 由于没有人提供更好的解决方案,我会接受我自己的答案。
    【解决方案3】:

    我现在发现这真的很痛苦。网格有时看起来很复杂,看起来毫无意义

    对于每个选定的单元格,我存储主键和网格列的名称(我创建了一个小类来保存它们)。

    然后我将它们全部放入一个列表中并遍历它们以进行更新。每次我更新一个单元格值时,我都会搜索实际单元格现在的位置,并将我的本地引用变量替换为该单元格,这样我就可以继续编写代码了。

           Cell.Value = ValueToWrite
           Cell = FindCell(Cell.OwningRow.DataGridView, DataRow, ColName)
    
    Function FindCell(Grid As DataGridView, DataRow As DataRow, ColName As String) As DataGridViewCell
        'Find the same cell, wherever you may be now, damn you sort.
        Dim GridRow = (From x As DataGridViewRow In Grid.Rows Where x.DataBoundItem.row Is DataRow).FirstOrDefault
        Dim Cell = GridRow.Cells(ColName)
        Return Cell
    End Function
    

    【讨论】:

      【解决方案4】:

      我遇到了这个问题,无法得到一个像样的答案,所以我尝试了这个,它成功了,

          private void SortBoundDG()
          {
              DataTable TempTable;
              TempTable = (DataTable)DG.DataSource;
              TempTable.DefaultView.Sort =  ColumnName + " " + "DESC";
              DG.DataSource = TempTable.DefaultView.ToTable();
          }
      

      只需将默认视图转换回表格并将其设置为数据网格视图的源

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多