473,508 Members | 3,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capture WinForm Datagrid After Sort

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

Jun 23 '06 #1
0 1216

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1293
by: zbcong | last post by:
hello in my winform,there is a datagrid,i want to fire some actions when the selected index is changed,but i fail to find the SelectedIndexChanged event to the winform datagrid,why? how can i...
1
2304
by: zbcong | last post by:
hello in my winform,there is a datagrid,i want to fire some actions when the selected index is changed,but i fail to find the SelectedIndexChanged event to the winform datagrid,why? how can i...
4
2868
by: Vai2000 | last post by:
Hi All, I have a dataview binded to a datagrid. I do some filter on the view, how can I get the filtered list displayed on the DG in my code The DG is on Winform TIA
3
2367
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
0
1445
by: Fabio R. | last post by:
With GDI+ I've made a little winform app to capture a webpage snapshot, using the CreateGraphic method of the Webbrowser control. Everything works fine in winform, but I need (by user input) URL...
3
3336
by: adh | last post by:
Please refer me to a well developed WinForm Hierarchical DataGrid sample code. I would like to save the cost and troubles of an 3'rd party winforms grid. Thanks, adh
3
13883
by: Melson | last post by:
hi can anyone help me how can i capture ENTER keystroke when the cell in datagrid is in editing mode. I'm now creating a data entry form with primary key in header and details in datagrid. So...
9
2698
by: astro | last post by:
I've tried the following without success....... AddHandler DS1.Tables("dial").RowChanged, AddressOf after_row_insert AddHandler CType(Me.dgDials.DataSource, DataTable).RowChanged, AddressOf...
1
2307
by: Jon B | last post by:
Hi All! I would like to make my WinForm DataGrid control so that user could easily edit the items in the **related table**. For instance, I have customers table and it has one-to-many...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.