locked
How to disable the column header sorting in DataGridView? RRS feed

  • Question

  • I did not see the "AllowSorting" property in DataGridView. How can I disable the header sorting? Thanks for your help!
    Tuesday, January 17, 2006 10:20 PM

Answers

  • Dim i As Integer

    For i = 0 To DataGridView1.Columns.Count - 1

        DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic

    Next i

    Wednesday, January 18, 2006 2:39 AM

All replies

  • Dim i As Integer

    For i = 0 To DataGridView1.Columns.Count - 1

        DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic

    Next i

    Wednesday, January 18, 2006 2:39 AM
  • Thanks it worked for me too

     

    Friday, January 5, 2007 9:35 AM
  • Hi

    To disable Sorting, u can write  "DataGridViewColumnSortMode.NotSortable;"

     

    Code is given Below:

    for (int i = 0; i < gvDirectorySubmission.Columns.Count - 1; i++)

    gvDirectorySubmission.ColumnsIdea.SortMode = DataGridViewColumnSortMode.NotSortable;

     

    Cheers, Cinni Patel

    Go straight n Turn Right

    Wednesday, April 18, 2007 6:27 AM
  • Use DGV's event ColumnAdded

     
           private void dgv_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
            {
                e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

    Or override OnColumnAdded function:

         protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
            {
                base.OnColumnAdded(e);
                e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
    Thursday, September 20, 2007 8:03 AM
  •  

    Isn't there any direct property of datgridview? Instead of using a loop.
    Thursday, January 31, 2008 12:21 PM
  • Go to Columns collection (Bottom of the grid view propriete), for each column make Sort Mode to NotSortable


    Thursday, March 13, 2008 11:00 PM
  • Pro version

     

    For Each dgvCol As DataGridViewColumn In Me.dgvTrace.Columns

        dgvCol.SortMode = DataGridViewColumnSortMode.NotSortable

    Next

    • Proposed as answer by tootsie cain Thursday, May 6, 2010 2:47 PM
    Wednesday, May 14, 2008 7:03 AM


  • 100%  

    Accurate ! no need for looping through datagrid columns





    Wednesday, June 25, 2008 11:41 AM
  •  

    Thanks... it's helpful
    Tuesday, August 5, 2008 7:38 AM
  • Thanks Dude....!!

    Its really Works for me...!!
    Wednesday, November 11, 2009 9:01 AM
  • Great Code.....

    100% Work

    Thanks.
    Wednesday, December 30, 2009 8:13 PM
  • Almost two years after! .. thanks this works!..

    no looping required.

     

    you rock.

    Thursday, May 6, 2010 2:46 PM
  • foreach is the same as looping, don't you think? :)
    Friday, May 28, 2010 11:21 PM
  • Thanx Duke

    You solved my numerous problems! Gr8 work!!!

    Thursday, August 12, 2010 2:13 PM
  • Right...Infact, i think foreach takes more resources than a for loop as it retrieves the Enumerator of the object collection and then loops through each object in the collection...
    Tuesday, May 31, 2011 9:27 AM