Results 1 to 3 of 3

Thread: How to prevent sorting in datagriview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    65

    How to prevent sorting in datagriview

    I have a datagridview named "dgvStudents" and DO NOT want user to click the heading to sort it.

    I found the following code but don't know how to incorporate in my code. Can someone please help ?

    Public Class CustomDataGrid
    Inherits DataGrid

    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    Dim point As New Point(e.X, e.Y)
    Dim hti As DataGrid.HitTestInfo = HitTest(point)

    ' don't sort column 1
    If hti.Type = HitTestType.ColumnHeader AndAlso hti.Column = 1 Then
    Return
    End If

    MyBase.OnMouseDown(e)
    End Sub

    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to prevent sorting in datagriview

    I think you should be able to just set the SortMode property of each column to NotSortable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to prevent sorting in datagriview

    Note that the code you posted is NOT for the DataGridView at all:
    Public Class CustomDataGrid
    Inherits DataGrid
    Different beast altogether.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width