?
Solved

Datagrideview Checkbox Column - Index Error

Posted on 2007-07-30
24
Medium Priority
?
797 Views
Last Modified: 2008-01-09
I get an index out of range error when I do the following on my check box column:

1. Select the 2nd from last record checkbox. The bindingsource filters it out of the datagridview.
2. The last row is automatically slelected. When I click it again I get this error: (see attached image)

Here is my code:


Code:
Private Sub TblShapeListDataGridViewUnBound_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblShapeListDataGridViewUnBound.CellClick
        Try
            If Not e.RowIndex = -1 Then
                If e.ColumnIndex = 2 Then
                    Dim chkFired As DataGridViewCheckBoxCell = CType((Me.TblShapeListDataGridViewUnBound(2, e.RowIndex)), DataGridViewCheckBoxCell)
                    If chkFired.GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.CurrentCellChange) = True Then
                        chkFired.Value = False
                        Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = DBNull.Value
                        Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value
                    Else
                        chkFired.Value = True
                        Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = OCNumber
                        Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = OCFileName
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.StackTrace)
        End Try

Ami Going about this all wrong??

Thanks,

Vwalla
0
Comment
Question by:vwalla
  • 15
  • 8
24 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 19595710
do u have really seven columns?
0
 
LVL 1

Author Comment

by:vwalla
ID: 19595755
Yes.  Since the 1st column is 0.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19595845
First, you should use CellContentClick when working with DataGridViewCheckboxColumn. On what line is the exception thrown? When exception is thrown, goto the immmediate window and write line by line

? e.ColumnIndex
? e.RowIndex
? TblShapeListDataGridViewUnBound.ColumnCount
? TblShapeListDataGridViewUnBound.RowCount

after every line you should press <Enter>, and then pass the values here you have received.

Goran
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:vwalla
ID: 19596703
It happens here:
Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = DBNull.Value
Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value
and here:
Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = OCNumber
 Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = OCFileName
These are used to either copy values into the cells from another selected row from another datagridview or remove them if unchecked.
The problem seems sporadic & associated with selecting the last rows check boxes.  I will look at the immediate window & post back

0
 
LVL 1

Author Comment

by:vwalla
ID: 19596709
Here was the immediate results:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
The expression cannot be evaluated while in run mode.

No other details that I could find on this...a bit ambiguous.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19596732
You didnt post what are the values of e.ColumnIndex, e.RowIndex, TblShapeListDataGridViewUnBound.ColumnCount and TblShapeListDataGridViewUnBound.RowCount from immediate window?

Goran
0
 
LVL 1

Author Comment

by:vwalla
ID: 19596753
There was not anything in the immediate window but what I posted.  Am I missing something?

V
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19596776
when exception is raised, and the code execution is stopped on

Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = DBNull.Value

the debugger is allowing you to change the code while the code is still running. Then you need to type this in immediate window

? e.ColumnIndex

and press <enter>, after which a value of ColumnIndex will be printed in immediate window. This u need to do for all 4 llines I have posted, and post here the resulting values from immediate window.

Goran
0
 
LVL 1

Author Comment

by:vwalla
ID: 19596832
OK...I got it now.

? e.ColumnIndex
5
? e.RowIndex
0
? TblShapeListDataGridViewUnBound.ColumnCount
8
? TblShapeListDataGridViewUnBound.RowCount
7
0
 
LVL 1

Author Comment

by:vwalla
ID: 19596976
I see one thing.  The ColumnCount is 8, when I only have 7 columns (0-6).  That sounds like it is an index out of range.
0
 
LVL 1

Author Comment

by:vwalla
ID: 19597024
Sorry...It is happening on the other datagridview.  Here is the immediate window:

? tblshapelistdatagridviewFired.ColumnCount
6
? tblshapelistdatagridviewFired.RowCount
0
? e.RowIndex
0
? e.ColumnIndex
5

Highlights:                      Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value

I only have 5 columns in my datagridview & the index is 6 ???
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19597040
ColumnCount must be at least 7 (which means you need to have at least seven columns), if you want to access column with index 6 (the last 7th column).

Goran
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19597051
It means that you only have 6 columns (indexed from 0 to 5), and you are trying to access column with index 6, which doesn't exist
]
Goran
0
 
LVL 1

Author Comment

by:vwalla
ID: 19597061
Sorry...my mistake again.  It  highlights this row:
 Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value

Where the column is 1...not 6
0
 
LVL 1

Author Comment

by:vwalla
ID: 19597067
                   Me.TblShapeListDataGridViewFired(1, e.RowIndex).Value = DBNull.Value
Sorry again...it is Monday & I cannot get anythig to work.

Anyways, column 1 exists b/c I have 6 columns (0-5).

Thanks,
V
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19598663
YOU have a fixed index in the code, and those where 5 and 6. Where did 1 came up now?

 Me.TblShapeListDataGridViewUnBound(1, e.RowIndex).Value = DBNull.Value ???????

You didnt post this lne anywhere until now

Goran
0
 
LVL 1

Author Comment

by:vwalla
ID: 19599113
It was happening on the code above that copies selected values into the datagrideview when the checkbox is true.   Here is what is happening (sorry for not being so clear, but I am seeing things now that were not so clear before).
I select a row in a parent datagrideview (tblOvenCharts).
In a tab control below resides 2 tables (tblShapeListDatagridviewUnBound & tblShapListDatagrideviewFired).
They are bound to 2 different bindingsources each that have a filter on them for CheckBox = True (tblShapesDataGrideviewFired) or CheckBox = False (tblShapeListDatagridviewUnBound).
When I check the checkbox in tblShapeListDatagridviewUnBound it copies OCNumber & OCFileName from tblOvenCharts columns at the selected row to the checked row in tblShapeListDatagridviewUnBound & then is filtered out of that gridview & shown in tblShapesDataGrideviewFired.  There were issues there when using CellContentClick (the same as above), but went away when I used CellValuesChanged.  When I go to tblShapesDataGrideviewFired & uncheck the box, the code Nulls the values for OCNumber & OCFileName and then the filter shows the row back on tblShapeListDatagridviewUnBound.  This is where the above error is popping up again.  Sorry for the confusion.  Below is my full code if you need it.  I will post some image links as to what my form looks like.

Thanks!

Public Class AssociateCharts
    Dim OCFileName As String
    Dim OCNumber As Integer

    Private Sub AssociateCharts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDatasets()
    End Sub

    Private Sub TblOvenChartsDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblOvenChartsDataGridView.CellClick
        Try
            If Not e.RowIndex = -1 Then
                OCFileName = Me.TblOvenChartsDataGridView(4, e.RowIndex).Value
                OCNumber = Me.TblOvenChartsDataGridView(0, e.RowIndex).Value
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub LoadDatasets()
        Me.TblShapeListTableAdapter.Fill(Me.DWGNumberDBDataSet.tblShapeList)
        Me.TblOvenChartsTableAdapter.Fill(Me.DWGNumberDBDataSet.tblOvenCharts)
    End Sub

    Private Sub TblShapeListDataGridViewFired_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
       
    End Sub


    Private Sub ButtonSaveClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveClose.Click
        Me.Validate()
        Me.TblShapeListBindingSource.EndEdit()
        Me.TblShapeListBindingSourceUnbound.EndEdit()
        Me.TblShapeListBindingSourceWNC.EndEdit()
        Me.TblShapeListTableAdapter.Update(Me.DWGNumberDBDataSet.tblShapeList)
        Me.Close()
    End Sub

    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        Me.Close()
    End Sub

    Private Sub TblShapeListDataGridViewUnBound_CellValueChanged1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblShapeListDataGridViewUnBound.CellValueChanged
        'Try
        If Not e.RowIndex = -1 Then
            If e.ColumnIndex = 2 And e.ColumnIndex < 8 Then
                Dim chkFired As DataGridViewCheckBoxCell = CType((Me.TblShapeListDataGridViewUnBound(2, e.RowIndex)), DataGridViewCheckBoxCell)
                If chkFired.Value = True Then
                    Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = OCNumber
                    Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = OCFileName
                ElseIf chkFired.Value = False Then
                    Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = DBNull.Value
                    Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value
                End If
            End If
        End If
        'Catch ex As Exception
        'MsgBox(ex.StackTrace)
        'MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub TblShapeListDataGridViewFired_CellValueChanged1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblShapeListDataGridViewFired.CellValueChanged
        'Try
        If Not e.RowIndex = -1 Then
            If e.ColumnIndex = 5 And e.ColumnIndex < 6 Then
                Dim chkFiredTrue As DataGridViewCheckBoxCell = CType((Me.TblShapeListDataGridViewFired(5, e.RowIndex)), DataGridViewCheckBoxCell)
                If chkFiredTrue.Value = False Then
                    Me.TblShapeListDataGridViewFired(0, e.RowIndex).Value = DBNull.Value
                    Me.TblShapeListDataGridViewFired(1, e.RowIndex).Value = DBNull.Value
                End If
            End If
        End If

        'Catch ex As Exception
        'MsgBox(ex.StackTrace)
        'MsgBox(ex.Message)
        'End Try
    End Sub

End Class

0
 
LVL 1

Author Comment

by:vwalla
ID: 19599157
0
 
LVL 1

Author Comment

by:vwalla
ID: 19599163
http://usera.imagecave.com/vwalla/Form1.jpg
This shows TblShapeListDataGridViewUnBound

http://usera.imagecave.com/vwalla/Form2.jpg
This shows TblShapeListDataGridViewFired
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 1000 total points
ID: 19599558
>> There were issues there when using CellContentClick (the same as above), but went away when I used CellValuesChanged.

You cannot use CellValueChanged with DataGridViewCheckBoxColumn, since it will not fire at all. And you shouldnt use CellClick either, because this event is fired if you click on the space in cell around the checkbox, without even changing the value of checkbox. The CellContectClick is the right event to use with DataGridViewCheckBoxColumn, exceptions that yo have been receiving have nothing to do with CellContentEvent behaviour, but with the wrong code in it.

>> TblOvenChartsDataGridView_CellClick

CellClick event is not good here because it is fired on every click, and will NOT fire if user uses UP/DOWN arrows to select a row, which can be a often case. Since you are using SelectionMode = FullRowSelect, the better option should be SelectionChanged event. So I would use this instead

    Private Sub TblOvenChartsDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TblOvenChartsDataGridView.SelectionChanged
        Try
            If Me.TblOvenChartsDataGridView.CurrentRow IsNot Nothing Then
                OCFileName = Me.TblOvenChartsDataGridView(4, Me.TblOvenChartsDataGridView.CurrentRow.Index).Value
                OCNumber = Me.TblOvenChartsDataGridView(0, Me.TblOvenChartsDataGridView.CurrentRow.Index).Value
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

You seem to still not understand how this e.ColumnIndex and DataGridView.ColumnCount works. YOu wrote this

If e.ColumnIndex = 2 And e.ColumnIndex < 8 Then

if e.ColumnIndex is 2, then e.ColumnIndex will without any doubt be less than 8, since 2 is less than 8. If e.ColumnIndex is not 2, it doesnt matter if it is 1 or 15, the code in if.. end if will not execute, since the first part of the condition -  if e.ColumnIndex = 2 is not true. So, the other part is not needed, just first one is enough

If e.ColumnIndex = 2 then
    ' this code will execute only and only if ColumnIndex = 2

The code for TblShapeListDataGridViewUnBound would look something like this


Private Sub TblShapeListDataGridViewUnBound_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblShapeListDataGridViewUnBound.CellContentClick
        'Try
        If Not e.RowIndex = -1 Then
            If e.ColumnIndex = 2 Then
                Me.TblShapeListDataGridViewUnBound(2, e.RowIndex).Value = Not Convert.ToBoolean(Me.TblShapeListDataGridViewUnBound(2, e.RowIndex).Value);
                If Convert.ToBoolean(Me.TblShapeListDataGridViewUnBound(2, e.RowIndex).Value) Then
                    Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = OCNumber
                    Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = OCFileName
                Else
                    Me.TblShapeListDataGridViewUnBound(5, e.RowIndex).Value = DBNull.Value
                    Me.TblShapeListDataGridViewUnBound(6, e.RowIndex).Value = DBNull.Value
                End If
            End If
        End If
        'Catch ex As Exception
        'MsgBox(ex.StackTrace)
        'MsgBox(ex.Message)
        'End Try
    End Sub

This code will work only if at the time of execution TblShapeListDataGridViewUnBound has at least 7 columns.

The similar code is for TblShapeListDataGridViewFired

Private Sub TblShapeListDataGridViewFired_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblShapeListDataGridViewFired.CellContentClick
        'Try
        If Not e.RowIndex = -1 Then
            If e.ColumnIndex = 5 Then
                Me.TblShapeListDataGridViewFired(5, e.RowIndex).Value = Not Convert.ToBoolean(Me.TblShapeListDataGridViewFired(5, e.RowIndex).Value);
                If Not Convert.ToBoolean(Me.TblShapeListDataGridViewFired(5, e.RowIndex).Value) Then
                    Me.TblShapeListDataGridViewFired(0, e.RowIndex).Value = DBNull.Value
                    Me.TblShapeListDataGridViewFired(1, e.RowIndex).Value = DBNull.Value
                End If
            End If
        End If
        'Catch ex As Exception
        'MsgBox(ex.StackTrace)
        'MsgBox(ex.Message)
        'End Try
    End Sub

And again, this code will work only if at the time of execution TblShapeListDataGridViewFired has at least 6 columns.

Now, I dont see the code for filtering data after the user checks and unchecks, could be that it is the cause of exceptions.

Goran
0
 
LVL 1

Author Comment

by:vwalla
ID: 19599617
The code for the filtiering the data was put directly into the bindingsource.  Should I "hard code" it after the cellcontentclick event?
With regards to the e.columnindex...that makes total sense.  I was trying to see if the event was looking for an extra column, but that was bad logic.  I will make the changes above & get back to you.

Thanks...looks like we are on the road to an answer here.
0
 
LVL 1

Author Comment

by:vwalla
ID: 19599719
tblOvenCharts is related to tblShapeListDatagridviewFired by OvenCycle.  There is no filter on the bindingsource for that tblShapeListDatagridviewFired, but only for tblShapeListDatagridviewUnBound.  Could this be of issue?  When I uncheck the Fired column in the Fired Shapes tab, they go to the Unbound datagridview (even though there is no filter).  I have narrowed down the issue to when I have 1 row left that I uncheck, I get the error.  I would assume that the filter had nothing to filter, but that particular bindingsource has no filter.
0
 
LVL 1

Author Comment

by:vwalla
ID: 19599786
OK.  Seems to be working now.
I will do some final testing & post back any issues, but your solution worked.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19599938
I am glad I could help

Goran
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question