1

This error comes out whenever I click the datagrid.

The program does fill the datagrid every time data was selected in the combobox.

For example, I choose data1 which has four records in datagrid, and then I click row index no 1. No problem, it will be shown, but when I choose another data again in combobox, for example, Data 2 has only one record then I will again click the datagrid.

This is the time that error will pop up.

Please see the code on how I fill the data grid:

Sub FillDtgPir(ByVal qry As String)
    Try
        If SQLConn1.State = ConnectionState.Closed Then SQLConn1.Open()
        Dim adap As New SqlDataAdapter(qry, SQLConn1)
        Me.DtsLineReq1.PRRMS_PIR.Clear()
        adap.Fill(Me.DtsLineReq1, "PRRMS_PIR")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Is there missing something in the code?

2 Answers 2

1

If the selected row index is larger than the largest index (number of rows minus one) in the new data, then decrease the row index as necessary before doing the fill? Or check the reason for the exception and decrease the row index instead of displaying the error message?

Sign up to request clarification or add additional context in comments.

Comments

0

When refilling the datagrid - make sure you reset the selected row (which is what I'd expect the Clear() to do).

An index outside the bounds of the array is usually caused by you trying to see a row that isn't there anymore. Do you have a pointer to the current row in the datagrid anywhere?

Is anything happening in the datagrid click event?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.