Reputation: 81
I have a checkbox column in my datagrid view, and I want to restrict the user into checking 7 boxes only, no more, no less. Also, I want something to happen on every checkbox click. Like this (pseudo-code only):
While counter is not equal to 7 {
Check if the user clicked a checkbox (say, some random checkbox 1) {
(when I say random, I mean not in order, or in no particular order)
copy row data from where the user clicked the checkbox onto another form (data 1)
increment counter to 1
display msgbox saying that the user clicked '1 out of 7'}
Check if the user clicked another random checkbox (2) {
copy row data from where the user clicked the checkbox onto another form (data 2)
increment counter to 2
display msgbox saying that the user clicked '2 out of 7'}
.
.
.
Check if the user clicked another random checkbox (7) {
copy row data from where the user clicked the checkbox onto another form (data 7)
increment counter to 7
display msgbox saying that the user clicked '7 out of 7'}
If counter is more than 7, exit sub and display msgbox('You can only select 7 names')
I have tried several layers and different arrangements of FOR_NEXT loops to get this to work but I just can't make it work! Here is my code:
Private Sub dgvPaidComms_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPaidComms.CellContentClick
Dim counter As Integer = 0
For Each row As DataGridViewRow In dgvPaidComms.Rows
Dim selected As Boolean = DataGridView1.Rows(e.RowIndex).Cells(0).Value
If selected = True Then
counter += 1
End If
Next
If counter > 7 Then
MsgBox("You can only select 7 names.")
Exit Sub
End If
End Sub
But what it does is loop through ALL of the rows in my DGV (which has 50+ rows) so it displays the MsgBox("You can only select 7 names.")
.
I have also tried using a normal FOR-NEXT loop (I mean, the normal counter thing. Like For counter As Integer = 0 to 7 ... Next
thing.) and putting the For Each row As DataGridViewRow In dgvPaidComms.Rows...
inside of it, and vice versa, only to get disappointed.
I am lost. I am really confused. I think it has something to do with the DataGridView1.Rows(e.RowIndex).Cells(0).Value
because I think it only captures one CellClick event (I mean, one checked checkbox. Because what happens when I try to run the code is, I check one checkbox, and I get MsgBox("You can only select 7 names.")
popping out because it really runs through all of the rows, and when I use the normal FOR_NEXT (explained above), It becomes an infinite loop.)
Can someone help me clear up this confusion?
Upvotes: 0
Views: 1732
Reputation: 38875
assume the DGV with the column is named "colCheck":
Private ChkCount As Integer = 0
Private Sub dgv_CellContentClick(sender As Object,
e As DataGridViewCellEventArgs) Handles dgv.CellContentClick
' check if this is from the check column
If dgv.Columns(e.ColumnIndex).Name = "colCheck" Then
' yes. so, is it checked or unchecked (user can do either)
If CType(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value,
Boolean) = True Then
ChkCount += 1 ' increment counter
' the "do something" goes here
If ChkCount = 7 Then
DoProcedureSeven() ' do what happens when they hit 7
End If
Else
ChkCount -= 1 ' if they uncheck, decrement counter
' be sure to UNDO something here
End If
End If
End Sub
You might want to rethink the whole copy row to somewhere at the time they Check something since they can also change their mind and uncheck one. This means you'd have to find the data copied before and remove it. Instead, just wait until they hit 7 checks, and copy all 7 rows at once (they will still be checked). Maybe by enabling a "Do Something" or "Finalize" button only when ChkCount
= 7.
When you are done doing whatever happens when they hit 7, your code should reset ChkCount
to 0, and clear the checks if they can do it again or redo it.
Upvotes: 1