0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
airhalynn101
  • 81
  • 2
  • 9

1 Answers1

1

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.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Oh, so it's just IF statements... It works now. By the way, I used the 'long form' because when I tried the 'CType' for the IF part, it returns errors but when I used the 'long form', it worked fine. Thanks! :) – airhalynn101 May 18 '14 at 13:41
  • I think I added that at the end just to try and avoid the HScroll for one long line (fixed, thanks). Its a *little* more than just IF statements. Your code loops thru all the rows for each click rather than just handling this click. If you copy the stuff as you see each check, you'd have massive duplicates in the destination. – Ňɏssa Pøngjǣrdenlarp May 18 '14 at 14:00
  • I get it now. I did as you suggested (copy the data after all the checking/unchecking so what I get is what I need, or something like that, but I understand it that way, and I hope I understood it as how you want me to.) Thank you. – airhalynn101 May 19 '14 at 03:11
  • I know that this question has been inactive for 11 days but I still have a question about this topic and I don't want to ask a new question as it may cause redundancy. So the check-uncheck thing has been working good for a while now, until I noticed that whenever I uncheck some rows (randomly, not in order) the fields (labels) where their values should have been are empty, thus, if I checked rows 1-5 and unchecked 3 and 2, the form where the values are copied have gaps in fields 2 and 3, which is ugly and wrong. What should I do to put them in order? (I want to see 1-2-3 instead of 1- - -4-5) – airhalynn101 May 29 '14 at 08:34
  • it does sound like a new question - this new one has to do with what the above code does *after* 7 are picked, and I have no idea what/how you copy the data from one to another. So I'd ask a new question with that code included. – Ňɏssa Pøngjǣrdenlarp May 29 '14 at 12:29
  • Oh, I see. And at least I can provide more details than just commenting here. Thanks. – airhalynn101 May 29 '14 at 12:58