[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1006
  • Last Modified:

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
0
Mike_Stevens
Asked:
Mike_Stevens
  • 6
  • 6
1 Solution
 
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now