Results 1 to 3 of 3

Thread: [RESOLVED] Index out of range exception

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    35

    Resolved [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    35

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width