1 Previous Next 

Notify that a Sort Event takes place


When you sort a DataGridView the dataview will fire a listchanged event. To be notified when the grid is sorted add a handler to the dataviews listchanged event. Here is some sample code.

Untitled 1

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object,

                           ByVal e As System.EventArgsHandles MyBase.Load

        Using da As New SqlDataAdapter("Select * From Products",

                                       "Server = YourServerName;Database" & _

                                       " = NorthWind;Integrated Security = SSPI;")

            Dim dt As New DataTable

            da.Fill(dt)

            DataGridView1.DataSource = dt.DefaultView

            AddHandler dt.DefaultView.ListChanged, AddressOf ListChanged

        End Using

    End Sub

 

    Private Sub ListChanged(ByVal sender As Object,

                            ByVal e As System.ComponentModel.ListChangedEventArgs)

        Dim dv = DirectCast(DataGridView1.DataSource, DataView)

        MessageBox.Show(String.Format("Sort on column {0}"dv.Sort))

    End Sub

End Class




1 Previous Next