Disable sort on specific columns with VB.NET datagrid

E

ECD

Hello all,

I can usually find solutions to my .NET problems by searching these
groups, but I'm stumped on this one. I have a datagrid in VB.NET (2.0
framework). I want to disable sorting on the first column in the grid
only. I havent found a way to reliably do this yet.

I tried putting the following code in the datagrid's mouse down event

Dim hti As DataGrid.HitTestInfo
hti =
dgCompList.HitTest(dgCompList.PointToClient(Control.MousePosition))

If hti.Type = DataGrid.HitTestType.ColumnHeader Then
If hti.Column = 0 Then
dgCompList.TableStyles(0).AllowSorting = False
dgCompList.AllowSorting = False
Else
dgCompList.TableStyles(0).AllowSorting = True
dgCompList.AllowSorting = True
End If
End If

This almost works, except the first time I click on the column heading
for row zero, the sort happens, but further clicks behave as expected
(no sorting). Its almost as though the sort event fires before I am
able to disable the sorting on the grid. Any ideas? Thanks in
advance.

Eric
 
E

ECD

Thanks, that did the trick. Here is the code I'm using to disable
sorting on column zero only:

Public Class CustomDataGrid
Inherits DataGrid

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

'This keeps column zero from being sorted
If hti.Type = DataGrid.HitTestType.ColumnHeader Then
If hti.Column = 0 Then
Return 'no baseclass call
End If
End If

MyBase.OnMouseDown(e)
End Sub

Public Sub New()

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top