【问题标题】:Preventing column auto sort after editing a bound DataGridView编辑绑定的 DataGridView 后防止列自动排序
【发布时间】:2009-06-26 13:42:16
【问题描述】:

我有一个 DataGridView,它有一个 DataTable 作为数据源。 每当我对列进行排序然后编辑单元格时,在编辑列之后,列会自动排序,因此最近编辑的单元格不再位于可查看区域中。有什么办法可以防止这种自动排序发生,并且只在我点击列时进行排序?

非常感谢。

达林

【问题讨论】:

    标签: vb.net sorting datagridview


    【解决方案1】:

    我认为没有办法阻止排序,但是您可以在更新前存储网格的行、列和排序值,然后在更新后恢复它们。

    这是我使用的一些代码:

    Friend Sub refreshGrid()
        Dim row As Integer
        Dim col As Integer
        Dim gridCol As Integer
        Dim gridColsToHide As Integer
        Dim rowCountOrig As Integer
        Dim rowCountNew As Integer
    
        row = dgvReadOnly.CurrentRow.Index
        col = dgvReadOnly.CurrentCell.ColumnIndex
            Dim sortVal As String
    
            sortVal = _setSource.DefaultView.Sort
            rowCountOrig = _setSource.Rows.Count
            _setSource = _displaySet.displaySetTable
            rowCountNew = _setSource.Rows.Count
            setRowFilter()
            _setSource.DefaultView.Sort = sortVal
            If (rowCountNew > rowCountOrig) Then  ' added new row
                row = dgvReadOnly.RowCount - 1  ' new row is at bottom of grid, but skip * row
            End If
        dgvReadOnly.DataSource = Nothing
        dgvReadOnly.DataSource = _setSource
        If row >= dgvReadOnly.RowCount - 1 Then   ' after delete
            row = dgvReadOnly.RowCount - 2
        End If
        If row >= 0 Then        ' if didn't delete all rows matching filter
            dgvReadOnly.CurrentCell = dgvReadOnly(col, row)
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      我不知道这是否有效,但听起来可能。

      尝试设置每个列的sortMode(DataGridView.Columns(1).sortMode)在选择单元格式编辑时,将其设置为自动完成编辑后,将其设置为“自动”。

      【讨论】:

        猜你喜欢
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2013-12-28
        相关资源
        最近更新 更多