Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB.NET
'Check box column index
Private CheckColIndex As Integer = 0
Private Sub DataGridView3_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellValueChanged
    'Check the column index and if the check box is checked.
    If e.ColumnIndex = CheckColIndex Then
        Dim isChecked As Boolean = CType(Me.DataGridView3(e.ColumnIndex, e.RowIndex).Value, Boolean)
        If isChecked Then
            'If check box is checked, uncheck all the rows, the current row would be checked later.
            For Each row As DataGridViewRow In Me.DataGridView3.Rows
                row.Cells(e.ColumnIndex).Value = False
            Next
        End If
    End If
End Sub


What I have tried:

I tried to search in internet but not found solution.
In my project my problem is for Dim isChecked As Boolean = CType(Me.DataGridView3(e.ColumnIndex, e.RowIndex).Value, Boolean)
---
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Posted
Updated 11-Apr-16 10:04am
Comments
ZurdoDev 11-Apr-16 13:56pm    
The error is very simple. You are trying to access something by index and the index you are trying to get to is not there. All you have to do is debug the code and see what the values are.
ionMEMBER 11-Apr-16 14:09pm    
I do not know if this code is for only one checkbox checked in datagridview or no because I search in internet and found this code. You have any idea ? thanks :)
ZurdoDev 11-Apr-16 14:18pm    
First off, you need to understand the code so that when it does not work, you can fix it.

Secondly, most of the time I see people reference a cell by doing grid.Rows[x].Cells[y].

Third, debug the code. What is the value of e.ColumnIndex? What is the value of e.RowIndex?
ionMEMBER 11-Apr-16 14:43pm    
datagridview is with 2 columns: currency , base currency
currency = string , base currency = boolean
I want in the column: "base currency" to be only one checkbox checked and no more
ZurdoDev 11-Apr-16 14:47pm    
What is the value of e.ColumnIndex? What is the value of e.RowIndex?

Perhaps you try it like this :
VB
Dim isChecked As Boolean = CType(Me.DataGridView3.Item(e.ColumnIndex, e.RowIndex).Value, Boolean)


you only can access an Item from the DGV by those Indexes ... ;-)
 
Share this answer
 
Comments
ionMEMBER 11-Apr-16 15:31pm    
I tried this but is the same problem
ionMEMBER 11-Apr-16 16:02pm    
THANKS MAN !! I FOUND SOLUTION :)
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Did you check that the object is not NULL and of size you expect ?
 
Share this answer
 

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