Determine when Datagridview checkbox column has been checked or unchecked

I have a datagridview on a windows form that the user will populate by clicking a button the form.  The first column in the datagridview is a checkbox column.  When the user checks the checkbox and the checkbox I need to run a function that  totals up the value of one column in the datagridview.  

I have everything figured our - populating the guide, formatting the datagridview so that the first column us a checkbox, performing the calculations, etc.    The probably I am having is determining  when the user checks or unchecks the checkbox so that I can execute the code the totals the table values.

I need to know what event to use or how to tell when the checkbox is either checked or unchecked so I can fire my code.   I tried the cellvaluechanged event but when the form is loading it fires the code and throws an error looking for a column in the grid that the code uses to total everything up.  

Any help would be appreciated
Mike_StevensAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You can use the cell value changed, just evaluate for your column:

if e.ColumnIndex = 0  then
      dim isChecked as Boolean = theGrid(0, e.rowIndex).FormattedValue
      if isChecked then
         ' do something
      end if
end if

Open in new window

0
Mike_StevensAuthor Commented:
I am formatting the grid by calling code in the page load event.   In that code is when the column names are assigned and the column count is established.  

when the page load I am getting a "index our of range" error.  I assume its because the grid is not formatted yet.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
for that I normally create a loading variable:

Dim Loading as Boolean = true

form_load()
   initGrid()
   ' other stuff
    Loading = false
end if

you can then do:

if not Loading andalso e.ColumnIndex = 0  then

end if

The other way to do it is to make sure you have the length already.  What are the values where you're getting the index out of range error?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Mike_StevensAuthor Commented:
Ok....I got the index range thing resolved....and just to test when the checkbox is clicked I have the following in the cellvaluechanged event.   Now every time I click the checkbox the msgbox, regardless if I am checking it or unchecking it the message box should appear right?

            If e.ColumnIndex = 0 Then
                 MsgBox("Checkbox in column 0 clicked!")
            End If

But it does not so my code does not get fired.  Every time the checkbox is checked I need my code to run. My code determines if the checkbox is checked (True) or not checked (False)

What am I missing?
0
Mike_StevensAuthor Commented:
I see that it fires but not the focus of the cell is lost which in my case is less than ideal
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Do you have any hidden columns?  Can you confirm the ColumnIndex is 0 for the checkbox column?

So we confirmed the event is firing but you're losing focus on what?  


Try adding a test TextBox called txtTest outside of the grid for now.

use this:                
             If e.ColumnIndex = 0 Then
                txtTest.Text = e.RowIndex.ToString()
            End If

Check row 2 (2 should appear in the text)
Check row 3 (3 should appear in the text)
uncheck row 2 (2 should appear in the text)
check row 4 (4 should appear in the text)
uncheck row 3 (3 should appear in the text).

Let me know if that works for you.
0
Mike_StevensAuthor Commented:
There are  no hidden columns and the check box column is the first column or 0.   By putting in breakpoints I can see that my code to does fire until after the cell that was just clicked loses focus.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
I'm sorry but I'm not following you.

So you click the checkbox, the event fires.  You then lose focus?

Can you explain in more detail what you're trying to accomplish?
0
Mike_StevensAuthor Commented:
The purpose of the checkbox column I am using is to indicate if the record displayed in the row should be included in the data when a report is generated.   The theory is that when the user "checks" the checkbox he is indicating to "Omit" that row from the data to be used in the report.

The code that is fires loops through the datagridview a calculates at total of the values in one column of the rows that have not been omitted (not checked) and then displayed the result in textbox on the screen.    All of my code works properly.    The problem is when you check the checkbox it checks or unchecks but the code that should get fired does not fire until you move focus from the cell in the grid.   So if the use check the box or unchecks it does not appear to have done anything because the value on the screen does not change.    If you check or uncheck the box and them click elsewhere on the form it see the total value changed because the code fired when the cell lost focus.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Understood.

Are you using a binding source to do the databinding?

  Dim myBinding as BindingSource = new BindingSource()
  myBinding .DataSource = MyDataTable
  myDataGridView.DataSource = myBinding

Open in new window


Also are you regenerating the total in the valuechanged event?
0
Mike_StevensAuthor Commented:
I am not using databinding. I bind the datagridview to dataset using codce   The grid is populated by the user clicking a button.  Then they can chose to omit a record.  The grid only gets populated when the user clicks a button.   The calculations are done using the values are actually in the datagridview.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.