I derived my own custom class from the datagrid class.
I overrode the ProcessCmdKey Function, like this, to catch the up and down
arrow keys:
======
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.Down
RaiseEvent key_down()
Return True
Case Keys.Up
RaiseEvent key_up()
Return True
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
======
I added these events:
======
Public Event key_down()
Public Event key_up()
======
In a form, I added my custom datagrid, and I added these subs to my form to
handle those events:
(ReportedTime is a datatable).
======
Private Sub DataGrid1_key_down() Handles Datagrid1.key_down
If Not ReportedTime.Rows.Count = 0 Then
If Datagrid1.CurrentCell.ColumnNumber = 1 Then
If
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4) >= 1 Then
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4)
= ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4) - 1
End If
ElseIf Datagrid1.CurrentCell.ColumnNumber = 2 Then
If
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5) >= 1 Then
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5)
= ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5) - 1
End If
End If
Datagrid1.DataSource = ReportedTime
End If
End Sub
Private Sub DataGrid1_key_up() Handles Datagrid1.key_up
If Not ReportedTime.Rows.Count = 0 Then
If Datagrid1.CurrentCell.ColumnNumber = 1 Then
If
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4) <= 9 Then
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4)
= ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(4) + 1
End If
ElseIf Datagrid1.CurrentCell.ColumnNumber = 2 Then
If
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5) <= 58 Then
ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5)
= ReportedTime.Rows(Datagrid1.CurrentCell.RowNumber) .Item(5) + 1
End If
End If
Datagrid1.DataSource = ReportedTime
End If
End Sub
======
However, the following problems have arisen:
1) If the datagrid is empty and I insert a numeric value into a cell, the
code within the two functions above is not evaluated since that datatable
has no rows yet. So, this basically makes the arrow keys worthless unless
the datagrid is already filled. Is there a way to change the cell's value
without having to access the datatable?
2) If there are two or more rows already in the datatable and displayed in
the datagrid, everything works great. The column increments via the up/down
arrow keys perfectly. However, if there is only one row in the datatable
(and of course, only one row displayed in the datagrid), it does not
increment, or at least not visually. The column only updates itself after I
click away from the active cell. So, I can increment it up or down all I
want, but I won't be able to know what the actual value I just created is
until I click away. The interesting thing about this single-row situation
is that if I click the asterisk in the datagrid (to create a new row), and
then click *back* to the original row (the new row disappears as normal),
the columns increment normally, and I can see how them do so in real time.
Any suggestions?
Thanks!
Matt