How to disable sort in DataGridView?

前端 未结 9 672
孤城傲影
孤城傲影 2020-12-04 23:35

How can I disable sort in DataGridView? I need to disable the header DataGridView sorting.

9条回答
  •  孤城傲影
    2020-12-04 23:42

    For extending control functionality like this, I like to use extension methods so that it can be reused easily. Here is a starter extensions file that contains an extension to disable sorting on a datagridview.

    To use it, just include it in your project and call like this

    myDatagridView.DisableSorting()

    In my case, I added this line of code in the DataBindingComplete eventhandler of the DataGridView where I wanted sorting disabled

    Imports System.ComponentModel
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    Public Module Extensions
    
    
    Public Sub DisableSorting(datagrid As DataGridView)
        For index = 0 To datagrid.Columns.Count - 1
            datagrid.Columns(index).SortMode = DataGridViewColumnSortMode.NotSortable
        Next
    End Sub
    
    
    End Module
    

提交回复
热议问题