469,632 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Disable sort on specific columns with VB.NET datagrid

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(Contro l.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

Dec 1 '06 #1
1 6005
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

Ken wrote:
Hi,

I think you are going to have to suppress the mouse down message
when the user is over the column header you did not want sorted like in this
tip here for preventing row and column resize.

http://www.vb-tips.com/dbpages.aspx?...9-526ea7d7382d

Since you are using .net 2.0 you could always use a datagridview instead of
a datagrid. You could prevent an column form being sorted like this

DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
Ken
-----------------

"ECD" wrote:
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(Contro l.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
Dec 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Brett P. | last post: by
reply views Thread by Rhodri | last post: by
5 posts views Thread by Jay Villa | last post: by
1 post views Thread by nate axtell | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.