Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid highlight cell 1

Status
Not open for further replies.

AMHCLH

Programmer
Nov 26, 2002
23
US
I have datagrid navigation set up, but I want to allow the user to move to the next row by pressing the arrow key after entering data(while still in the cell) and move to the next row same column. I capture the key stroke and it moves to the next row, but doesn't highlight that particular cell.

Datagrid1.setfocus
Datagrid1.row = Datagrid1.row + 1
Datagrid1.col = DataGrid1.col


Any ideas?
 
Datagrid1.columns(Datagrid1.col).SelStart = 0
Datagrid1.columns(Datagrid1.col).SelLength = Datagrid1.columns(Datagrid1.col).Text
 
CCLINT,

Thanks for responding. I tried the change and get an error that the method or data member (.selstart) is not found.
 
sorry. try:

Datagrid1.SelStart = 0
Datagrid1.SelLength = Datagrid1.columns(Datagrid1.col).Text
 
CCLINT,

It moves to the end of the right row, but doesn't position the cursor in the cell. Here is my code:

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
DataGrid1.Row = DataGrid1.Row + 1
DataGrid1.Col = DataGrid1.Col
DataGrid1.SelStart = 0
DataGrid1.SelLength = DataGrid1.Columns(DataGrid1.Col).Text
End If
End Sub

Thanks...
 
1. You shouldn't have to do this in a DataGrid if the MarqueeStyle is set to:

datagrid1.MarqueeStyle=dbgFloatingEditor

If you are using a MarqueeStyle of something like dbgSolidCellBorder, then use this in the RowColChange Event instead:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
If DataGrid1.col>=0 Then
DataGrid1.EditActive = True
DataGrid1.SelStart = 0
DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.col).Text)
End If
End Sub

2. Doing this in the keydown event is wrong. Your row will also move 2x instead of 1x

3. This:

DataGrid1.SelLength = DataGrid1.Columns(DataGrid1.Col).Text

Is not the code I posted. You will get an error if the column text is not numeric. The code I posted uses Len()




 
Change the code for #1 to the following, so that it gets used at the proper time:
Code:
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    If Not IsNull(LastRow) Then
        DataGrid1.EditActive = True
        DataGrid1.SelStart = 0
        DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.col).Text)
    End If
End Sub
 
I tried changing datagrid1.MarqueeStyle=dbgFloatingEditor.

That didn't work. But removing the increment of the row/column did. Here is the working code:

Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
DataGrid1.SelStart = 0
DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.Col).Text)
End If
If KeyCode = vbKeyUp Then
DataGrid1.SelStart = 0
DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.Col).Text)
End If
If KeyCode = vbKeyRight Then
DataGrid1.SelStart = 0
DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.Col).Text)
End If
If KeyCode = vbKeyLeft Then
DataGrid1.SelStart = 0
DataGrid1.SelLength = Len(DataGrid1.Columns(DataGrid1.Col).Text)
End If
End Sub

Thanks, CCLINT! You get a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top