Datagrid how to sort by column header?

  • Thread starter Thread starter RB
  • Start date Start date
R

RB

If the user clicks on a column header can I sort by that column?

I would like to be able to still trap the click event for a click on any
other row. Any code would be appreciated.

I am using:
---------------
Private Sub CustomersDataGrid_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CustomersDataGrid.Click

Dim customersEditViewDialog As PocketRep6.CustomersEditViewDialog =
PocketRep6.CustomersEditViewDialog.Instance(Me.CustomersBindingSource)

customersEditViewDialog.ShowDialog()

End Sub
 
Hi RB,

First, you are using datagrid.Click event not as in this example
datagrid.MouseDown and datagris.MouseUp. In down and Up events you got
information about "click position" and base on this you can determine
what have been clicked: header or datagrid row.

I've got similar problem and I solved it by re-binding grid
datasource, because I'm using ResultSet and not dataset as in this
example.

Regards,

M. Wolniewicz
 
Maciej:

Thank you for your reply.

I can't get the code referenced below to work at all with the mouse events
either. So, I still need code to tell where (x, y) the user tapped to
invoke the mouse down and up events. I know that there has to be an easy
way to do this.

Any additional help would be appreciated.

RB

--
remove underscore to send me mail, no spam
Maciej Wolniewicz said:
Hi RB,

First, you are using datagrid.Click event not as in this example
datagrid.MouseDown and datagris.MouseUp. In down and Up events you got
information about "click position" and base on this you can determine
what have been clicked: header or datagrid row.

I've got similar problem and I solved it by re-binding grid
datasource, because I'm using ResultSet and not dataset as in this
example.

Regards,

M. Wolniewicz
 
Hi,

as I mension priviewsly in your code you try to use datagrid.Click
event. In this event you cannot check position where data drid was
clicked. Instead of Click event use MouseDown or MouseUp events. Below
you've got sample code:

Private Sub CustomersDataGrid_MouseDown(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
CustomersDataGrid.MouseDown
Dim hitTest As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)

If (hitTest.Row <> -1) Then

Else

End If

End Sub

Checking HitTestInfo object propertis you can test what was clicked.
More detaild description of this object you will find under this link:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagrid.hittestinfo.aspx

Regards,
M. Wolniewicz

Maciej:

Thank you for your reply.

I can't get the code referenced below to work at all with the mouse events
either. So, I still need code to tell where (x, y) the user tapped to
invoke the mouse down and up events. I know that there has to be an easy
way to do this.

Any additional help would be appreciated.

RB

--



First, you are using datagrid.Click event not as in this example
datagrid.MouseDown and datagris.MouseUp. In down and Up events you got
information about "click position" and base on this you can determine
what have been clicked: header or datagrid row.
 
Maciej:

The mouse down and mouse up events work. The mouse down instantiates the
focus on the datagrid and the mouse up event handles it by the following:
'-------------------------------
If hitTest.Type = dataGrid.HitTestType.ColumnHeader Then

i = hitTest.Column

columnName =
Me.CustomersTableStyleDataGridTableStyle.GridColumnStyles(i).MappingName

If Me.CustomersBindingSource.Sort = columnName Then

Me.CustomersBindingSource.Sort = columnName + " DESC"

Else

Me.CustomersBindingSource.Sort = columnName

End If

End If

----------------------

This works but, if soemone knows a better way to get the column name I would
like to know.

Thanks,

RB
=========
Hi,

as I mension priviewsly in your code you try to use datagrid.Click
event. In this event you cannot check position where data drid was
clicked. Instead of Click event use MouseDown or MouseUp events. Below
you've got sample code:

Private Sub CustomersDataGrid_MouseDown(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
CustomersDataGrid.MouseDown
Dim hitTest As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)

If (hitTest.Row <> -1) Then

Else

End If

End Sub

Checking HitTestInfo object propertis you can test what was clicked.
More detaild description of this object you will find under this link:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagrid.hittestinfo.aspx

Regards,
M. Wolniewicz

Maciej:

Thank you for your reply.

I can't get the code referenced below to work at all with the mouse events
either. So, I still need code to tell where (x, y) the user tapped to
invoke the mouse down and up events. I know that there has to be an easy
way to do this.

Any additional help would be appreciated.

RB

--
remove underscore to send me mail, no spam"Maciej Wolniewicz"



First, you are using datagrid.Click event not as in this example
datagrid.MouseDown and datagris.MouseUp. In down and Up events you got
information about "click position" and base on this you can determine
what have been clicked: header or datagrid row.
 
Hi,

it is good to know that it works:). You are using GridTableStyle, so I
think there is no other way to get column name.

M. Wolniewicz






Maciej:

The mouse down and mouse up events work. The mouse down instantiates the
focus on the datagrid and the mouse up event handles it by the following:
'-------------------------------
If hitTest.Type = dataGrid.HitTestType.ColumnHeader Then

i = hitTest.Column

columnName =
Me.CustomersTableStyleDataGridTableStyle.GridColumnStyles(i).MappingName

If Me.CustomersBindingSource.Sort = columnName Then

Me.CustomersBindingSource.Sort = columnName + " DESC"

Else

Me.CustomersBindingSource.Sort = columnName

End If

End If

----------------------

This works but, if soemone knows a better way to get the column name I would
like to know.

Thanks,

RB

Hi,

as I mension priviewsly in your code you try to use datagrid.Click
event. In this event you cannot check position where data drid was
clicked. Instead of Click event use MouseDown or MouseUp events. Below
you've got sample code:

Private Sub CustomersDataGrid_MouseDown(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
CustomersDataGrid.MouseDown
Dim hitTest As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)

If (hitTest.Row <> -1) Then

Else

End If

End Sub

Checking HitTestInfo object propertis you can test what was clicked.
More detaild description of this object you will find under this link:http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagri...

Regards,
M. Wolniewicz

Thank you for your reply.
 
Back
Top