Home All Groups Group Topic Archive Search About

Disable sort on specific columns with VB.NET datagrid

Author
1 Dec 2006 2:14 PM
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

Author
1 Dec 2006 5:58 PM
Ken Tucker [MVP]
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?ID=c129553e-a2c2-4838-9a79-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
-----------------

Show quoteHide quote
"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(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
>
>
Author
1 Dec 2006 7:09 PM
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:
Show quoteHide quote
> 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?ID=c129553e-a2c2-4838-9a79-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(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
> >
> >