Allow sorting in datagrid by specific columns

  • Thread starter Thread starter Rob Oldfield
  • Start date Start date
R

Rob Oldfield

VS 2003, framework 1.1

I have a datagrid on a windows form that uses a dataview as its datasource.
It's currently sortable by all columns. Is there any way to limit it so
that it is only sortable by specific columns?

The problem is that the automatic (i.e. change a value in a sorted column
and it immediately resorts) sorting in the other columns is confusing the
users, so an alternative question is: is there any way of turning that
automatic sorting off.

Thank you.
 
OK. Figured it out for myself...

In the click event of the datagrid added...

OldSort = dv.sort

Where OldSort is an accessible string variable.

Then in the dataview ListChanged event...

Dim NewSort As String
Dim pt As Point =
Me.PortfolioAllocationDG.PointToClient(Control.MousePosition)
Dim hti As DataGrid.HitTestInfo = Me.PortfolioAllocationDG.HitTest(pt)
If hti.Type = DataGrid.HitTestType.ColumnHeader Then
Select Case hti.Column
Case 3
If OldSort = "Code" Then
NewSort = "Code desc"
Else
NewSort = "Code"
End If
Case 4
If OldSort = "FullName" Then
NewSort = "FullName desc"
Else
NewSort = "FullName"
End If
Case Else
NewSort = OldSort
End Select
End If
dv.Sort = NewSort

Where Code and FullName are the two fields I want sortable and 3 & 4 are
their respective columns in the datagrid.

Any improvements would be welcome.
 
Back
Top