Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there i have problem checking current cell on my datagrid. my goal was to make a checker on the current cell of the datagrid whenever a press tab on the datagrid. but im having difficulty on my code here.

heres my code:

VB
Private Sub grdItems_KeyDown(sender As Object, e As KeyEventArgs) Handles grdItems.KeyDown
        
        Dim a, b As Integer 

        If e.KeyCode = Keys.Tab Then
            a = Me.grdItems.CurrentCell.ColumnIndex
            b = Me.grdItems.CurrentCell.RowIndex

            If Me.grdItems.CurrentCell = Me.grdItems(a, b) Then
                MessageBox.Show("next row")
            End If
        End If
    End Sub


but im just getting an whenever i check datagrid cell.. sorry for my bad english.. thanks in advance.. :)
Posted
Comments
What is a checker? Your problem is not clear too. When you debug, what you found?

To get a collection of DataGridViewItems (I hope your 'grdItems' is standard DataGridView???) you have to use grdItems.Items(). But if you set a('ColumnIndex') and b('RowIndex') the same as CurrentCell's Col- and Row- indexes, your
VB
Me.grdItems.CurrentCell = Me.grdItems.Items(a, b)
will always True. And everytime you will get a MsgBox.
I don't understand what your project does, but it seems to me that you mean an independent 'CurrentCell' which ColumnIndex and RowIndex are predefined variables. And when user moves through a collection of DataGridViewItem (using 'Tab' key), you want to catch if coordinates of your 'CurrentCell' will the same as coordinates of grdItems.CurrentCell.
Right?.. But what for?
DataGridView offers a lot of useful events, properties and methods that allow you to work with different types of cells, their decoration and values.
 
Share this answer
 
VB
' Try this code .....


Dim x As Integer
Dim y As Integer
x = Me.FadingGridView.CurrentCell.RowIndex
y = Me.FadingGridView.CurrentCell.ColumnIndex
If e.KeyCode = Keys.Tab Then
    If FadingGridView.Rows(x).Cells(0).Selected = True Then
        Me.FadingGridView.CurrentCell = Me.FadingGridView(x, y)
    End If
End If
 
Share this answer
 
thank you sir for the solution.. but i could not get it though.. hehe.. any way my goal was for example i have a barcode column and product column on my datagrid. whenever i am on barcode column whenever i press enter, the cell that should be on focus was the product column. but the focus just go to another barcode column which is a new row again. i just want my focus to be on the product column..

heres my code..


VB
Private Sub grdItems_KeyDown(sender As Object, e As KeyEventArgs) Handles grdItems.KeyDown
        
        Dim x As Integer

        'GET DATAGRID ROW AND COLUMN INDEX
        x = Me.grdItems.CurrentCell.RowIndex

       

        'THIS IS FOR RETURN FUNCTION
        If e.KeyCode = Keys.Return Then
            If grdItems.Rows(x).Cells(0).Selected = True Then
                Me.grdItems.CurrentCell = Me.grdItems(x, 1)
            End If
        End If
    End Sub


i have also tried your code but it just wont go to what i wanted the focus to go to. help pls.. thanks in advance..
 
Share this answer
 
1. Standard DataGridView has "EditMode" property. (What turns EditMode on - F2, Enter, and so on)
2. You can set DataGridViewColumn containig barcodes as ReadOnly
3. You can place Barcodes in RowHeaders to make them UNSELECTABLE.
4. you can Cancel of TabKey by catching this event:
VB
Private Sub DataGridView_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView.KeyDown
    If e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = False
    End If
End Sub



As I said before, standard DataGridView is rich of props,methods and events.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900