[RESOLVED] Index out of range exception
Hi Techies
I recently added code to a form event called Shown to make certain cells of each row of my datagridview read-only. Then I set the focus of the datagridview to the last entry of the dgv with the following code.
dgvCashJournal.Focus()
dgvCashJournal.CurrentCell = dgvCashJournal(0,
dgvCashJournal.RowCount - 1)
The problem I am having is I get an index out of range exception when I attempt to do anything after the dgv is displayed. I cannot exit the routine or refresh the data. The person who gave me the original code is no longer available. I know I need to put some code in place to handle this exception but don't know where or how. Can anyone help?
Papiens
Re: Index out of range exception
Just put your code in a If block like this, that is you only set the currentcell if you have at least a row in your datagridview.
Code:
Dim rowCount As Integer = dgvCashJournal.Rows.Count
If rowCount > 0 Then
dgvCashJournal.Focus()
dgvCashJournal.CurrentCell = dgvCashJournal.Item(0, rowCount - 1)
End If
Re: [RESOLVED] Index out of range exception
stanav
thanks for the heads up. I have already resolved this issue and forgot I had this request out. Thanks for your time.
Papiens