PC Review


Reply
Thread Tools Rate Thread

Capture WinForm Datagrid After Sort

 
 
crferguson@gmail.com
Guest
Posts: n/a
 
      23rd Jun 2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Refined Sort - Sort within a Sort Bishop Microsoft Excel Programming 0 7th Apr 2009 09:00 PM
Sort, and then sort from the first sort =?Utf-8?B?S0I=?= Microsoft Access 9 9th Jun 2006 04:07 PM
Winform>GUI>hide the frame in winform ? Tom Microsoft C# .NET 3 13th Sep 2004 02:25 PM
Datagrid (Winform) / Datatable / Sort problem Daniel Microsoft ADO .NET 0 30th Nov 2003 05:59 PM
Re: Datagrid - Capture sort Joe White Microsoft Dot NET Framework Forms 0 1st Sep 2003 09:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:40 PM.