I've been looking in every place I can to find a solution for this and
I believe I've pieced a solid one together so I thought I'd share since
I've found so much help on these groups...
This is for winforms only as I haven't tried anything like this in
ASP...
- I've added a boolean property to the form in the General Declarations
section:
Private IsSorted as Boolean = False
- You need to capture the fact that a column header was clicked in the
datagrid. I use the mousedown event:
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
Dim pt As Point = Me.DataGrid1.PointToClient(Cursor.Position)
Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(pt)
'check to see if the click was on a column header; if so set
IsSorted=True
If hti.Type = DataGrid.HitTestType.ColumnHeader Then
IsSorted = True
End If
End Sub
- Now, you need a Sub for handling what you want to do AFTER the list
is sorted:
Private Sub DatagridSorted(ByVal sender As Object, ByVal e As
System.ComponentModel.ListChangedEventArgs)
'check the Boolean to make sure the ListChange was caused by
sorting
If Me.IsSorted = False Then Exit Sub
'Do whatever events you want as a result of the sort
CodeIWouldWantToExecute()
'Toggle the Boolean back to False
Me.IsSorted = False
End Sub
- Last, add the Sub as a handler to the datagrid's ListChanged event.
You can put this in the Form's Load event after the datagrid is
bound... or wherever it is in your code that the datagrdid gets bound
to its datasource. This is just an example using the Load event:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dv as DataRowView, dt as DataTable
'code here that binds the datagrid to its datasource, and
then...
dt = Me.DataGrid1.DataSource
dv = dt.DefaultView
AddHandler dv.ListChanged, AddressOf DatagridSorted
End Sub
- One note about the last part. This can be done whether or not you
used a DataView or a DataTable as your grid's datasource. The above
code is if you used a DataTable. If you used a DataView then you can
go ahead and set the DataView straight to the grid's datasource (dv =
DataGrid1.DataSource)...
I hope this helps someone out there. If you respond to this post with
questions and I don't respond somewhat promptly, feel free to email me
at crferguson AT gmail.com
Cory