Datagrid Navigation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Does anyone know how to stop a user from navigating the cells in a datagrid
with the arrow keys or tab. Is there maybe a property for this? I have
searched but cannot find one.

Also, is there a way of sorting the columns programmatically. I want to be
able to sort them in code myself. I have got code implementing the hittest
method so I would be able to turn the AllowSorting property off and then pick
up whenever the user clicks on a column header and handle the sorting myself,
the problem is however that I wont be able to show the column as been sorted
i.e. the little up/down arrows. Any ideas?

Thanks in advance.
 
I face the same issue a month ago n found a perfect code from a forum, try
this.

Private ColumnHeaderPress As Boolean = False
Private ColumnCellPress As Boolean = False

Private ProductColumnPress As Boolean = False
Private ProductManagerColumnPress As Boolean = False

Private Sub grdMyProducts_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles grdMyProducts.MouseUp
Dim hitTest As DataGrid.HitTestInfo
Dim dataView As DataView
Dim columnName As String
Dim dataGrid As DataGrid

If (e.Button = MouseButtons.Left) And ColumnHeaderPress Then
'Reset mouse button state.
ColumnHeaderPress = False

'A datagrid object called this event handler.
dataGrid = CType(sender, DataGrid)

'Perform a hit test to determine where the mousedown event
occured.
hitTest = grdMyProducts.HitTest(e.X, e.Y)

'If the mousedown event occured on a column header,
'then perform the sorting operation.
If hitTest.Type = dataGrid.HitTestType.ColumnHeader Then
'Get the DataTable associated with this datagrid.
'dataTable = (DataTable)grdMyDoctorsNWDoctors.DataSource;

'Get the DataView associated with the DataTable.
'dataView = dataTable.DefaultView;
dataView = CType(grdMyProducts.DataSource, DataView)

'Get the name of the column that was clicked on.
'columnName = dataTable.Columns[hitTest.Column].ColumnName;
columnName = dataView.Table.Columns(hitTest.Column).ColumnName

'If the sort property of the DataView is already the current
'column name, sort that column in descending order.
'Otherwise, sort on the column name.
If dataView.Sort = columnName Then
dataView.Sort = columnName + " DESC"
Else
dataView.Sort = columnName
End If
End If
End If
End Sub
Private Sub grdMyProducts_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles grdMyProducts.MouseDown
Dim hitTest As DataGrid.HitTestInfo

'Utilize only left mouse button clicks
If (e.Button = MouseButtons.Left) Then
'A datagrid object called this event handler.
'grdMyDoctorsNWDoctors = (DataGrid)sender;

'Perform a hit test to determine where the mousedown event
occured.
hitTest = grdMyProducts.HitTest(e.X, e.Y)

'If the mousedown event occured on a column header,
'then perform the sorting operation.
If (hitTest.Type = DataGrid.HitTestType.ColumnHeader) Then
'Reset mouse button state
ColumnHeaderPress = True
Else
If hitTest.Column = 0 Then
ProductColumnPress = True
elseIf hitTest.Column = 2 Then
ProductManagerColumnPress = True
elseIf hitTest.Column = 6 Then
ColumnCellPress = True
End If
End If
End If
End Sub
 
Back
Top