hi,

i want to ask about the check/uncheck the checkbox in a column in datagrid.

if all row in the checkbox column is not selected, what is the code is....

because i want the msgbox appear if the user not check any row.

tq,

you can go through the whole datagrid (meaning the rows in the datagrid) assigning the checked of the checkbox column. At the end of the cycle, if any of the ceckboxes is checked, your boolean will be true.

Like this:

Dim check As Boolean
For i as Integer = 0 To dataGrid1.rows.count
    check = datagrid1.rows[i].cells["checkbox"].checked 'checkbox being the name of the column
Next
If check Then
    MessageBox.Show("You didn't check any checkbox")
End If

Edited 5 Years Ago by Nichito: n/a

thanks for your help.But

when i type:
check = datagrid1.rows.cells["checkbox"].checked 'checkbox being the name of the column

the error appeared that :
value of type 'system.windows.forms.datagridviewrowcollection' cannot be converted to 'boolean'

Sorry... Let me correct that code. (I got mixed up the language :P)

Dim check As Boolean
For i as Integer = 0 To dataGrid1.rows.count
    check = DataGridView1.Rows(i).Cells("checkbox").Value 'checkbox being the name of the column
                                                        'you can access it by index as well
Next
If check Then
    MessageBox.Show("You didn't check any checkbox")
End If

tq.but this error appeared:

{"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"}

what is the mean and how to solve it?

the index is the column number in the cell array (which composes the row). Each cell has a header (or title) which was specified when you specified how many (or which) columns were in the datagrid.

If you don't know what's the name of the column, simply enter (instead of .Cells["checkbox"] ) the number of the column (starting your count by 0) .Cells[0]

i see.i got it.

why the error appear?

{"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"}

the column name is chk in the first field.
i have 4 row in DataGridView1. it reads for these 4 row,but it keep read to the next row and the error appears.

the code like this:


Dim check As Boolean
For i As Integer = 0 To DataGridView1.Rows.Count
check = DataGridView1.Rows(i).Cells("chk").Value 'checkbox being the name of the column
'you can access it by index as well
Next
If check Then
MessageBox.Show("You didn't check any checkbox")
End If

Oh... I see the problem. Change the following line:

For i as Integer = 0 To dataGridView1.Rows.Count - 1

Since the array of rows starts from 0, and in the case of your datagridview ends in 3 (0, 1, 2, 3)

Edited 5 Years Ago by Nichito: n/a

After changing the range for the cycle? Make sure it stops at dataGridView1.Rows.Count -1 .

Try putting a Breakpoint (for debugging) at the start of the cycle so you can check how many times it is repeating and thus identify the error's cause

You are just missing the validation

Dim check As Boolean
    For i as Integer = 0 To dataGrid1.rows.count

    check = DataGridView1.Rows(i).Cells(0).Value

    If check Then
        Exit For
    End If

    Next

    If Not check Then
        MessageBox.Show("You didn't check any checkbox")
    End If
This question has already been answered. Start a new discussion instead.