DataGrid - can you tell what column is currently used foir the sort?

J

Jim

Hello,

I am creating a windows form application using vb.net 2003 which displays
data in a datagrid, and then, if the user wants, I produce a nicely
formatted crystal report of the data.

The question I have is: if I allow the user to sort the datagrid by
clicking on the column header, is there any way that I can tell which column
the datagrid is sorted on, so that I can sort the data in the crystal report
the same way?

I have not been able to find anything which tells me the current sort for
the datagrid, nor can I figure out how to override the header column click
so that I can set a variable in it...

Any help would be appreciated!

Thanks!

Jim
 
K

Ken Tucker [MVP]

Hi,

Add a handler to the dataviews list changed event. If you are using
a datatable as the datasource use the datatable's defaultview list changed
event.

Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da, daEmployees As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

daEmployees = New OleDbDataAdapter("Select * From Employees Order by
LastName, FirstName", conn)

daEmployees.Fill(ds, "Employees")

DataGrid1.DataSource = ds.Tables("Categories")

DataGrid2.DataSource = ds.Tables("Employees")

AddHandler ds.Tables("Employees").DefaultView.ListChanged, AddressOf
ListChanged

End Sub



Private Sub ListChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.ListChangedEventArgs)

Dim hti As DataGrid.HitTestInfo

Dim pt As Point

pt = DataGrid2.PointToClient(Me.MousePosition)

hti = DataGrid2.HitTest(pt)

Trace.WriteLine(String.Format("Sort on column {0}", hti.Column))

End Sub



Ken

------------------------
Hello,

I am creating a windows form application using vb.net 2003 which displays
data in a datagrid, and then, if the user wants, I produce a nicely
formatted crystal report of the data.

The question I have is: if I allow the user to sort the datagrid by
clicking on the column header, is there any way that I can tell which column
the datagrid is sorted on, so that I can sort the data in the crystal report
the same way?

I have not been able to find anything which tells me the current sort for
the datagrid, nor can I figure out how to override the header column click
so that I can set a variable in it...

Any help would be appreciated!

Thanks!

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top