Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning.

I have a problem with my datagrid when I do automatic row switching. In an invisible column, it shows me an exception: the active cell cannot be set as an invisible cell.

Please, what can I do to avoid this?

What I have tried:

Public Class Form1
    Dim row As Integer
    Dim column As Integer
    Dim modeEdition As Boolean
    Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then
            row = DataGridView1.CurrentCell.RowIndex
            column = DataGridView1.CurrentCell.ColumnIndex
            column += 1
            If column = DataGridView1.ColumnCount Then
                row += 1
                If row = DataGridView1.Rows.Count Then
                    If row = DataGridView1.RowCount Then
                        If DataGridView1.DataSource IsNot Nothing Then
                        End If
                    End If
                End If
                column = 0
            End If
            DataGridView1.CurrentCell = DataGridView1.Rows(row).Cells(column)
            e.Handled = True
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.Columns(2).Visible = False
    End Sub
End Class
Updated 18-May-24 2:39am
Dave Kreskowiak 18-May-24 9:56am    
What on earth is "automatic row switching"??

If you're talking about moving the CurrentCell, or moving the selection, that might make more sense to people.
HichSkill 18-May-24 16:11pm    
I'm talking about moving CurrentCell

1 solution

Check to see if the column is Visible before you set it as the active cell. If it isn't, you need to find a Visible column first ...
Share this answer
HichSkill 18-May-24 16:20pm    
I have a column sometimes that I want to hide it with Checkbox
OriginalGriff 19-May-24 1:15am    
Yes, but if it's invisible, it can't be the current cell because the user can't access it!
HichSkill 19-May-24 12:14pm    
I saw this method in commercial management software which hides the price excluding VAT column so as not to disturb it
OriginalGriff 19-May-24 12:21pm    
Yes but you still have to check!

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