5 Replies - 19610 Views - Last Post: 15 January 2013 - 08:12 AM Rate Topic: -----

#1 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 02:59 AM

hi,
the first visible column of my datagridview is a checkbox . the gridview has 3 rows, and i want something to hapen when the user checks and unchecks the checkbox of the second row.

i found out the proper method to do that, but i get an error like "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

here is my code:

  Private Sub ApMICDestinationTypesDataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ApMICDestinationTypesDataGridView.CellValueChanged
        Dim destination_abroad As Boolean = ApMICDestinationTypesDataGridView.Rows(1).Cells(2).Value

        If destination_abroad = True Then

            GroupTypeIDLabel.Visible = True
            GroupTypeIDComboBox.Visible = True
else 
            GroupTypeIDLabel.Visible = False
            GroupTypeIDComboBox.Visible = False

        End If
    End Sub




thanks a lot

Is This A Good Question/Topic? 0
  • +

Replies To: catch the check change event in the datagridview checkbox column

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9196
  • View blog
  • Posts: 34,536
  • Joined: 12-June 08

Re: catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 07:57 AM

I would probably put a breakpoint at line 02 and make sure you can access that specific row and cell.

02	        Dim destination_abroad As Boolean = ApMICDestinationTypesDataGridView.Rows(1).Cells(2).Value



An 'Index was out of range.' typically happens on a collection.. arrays, lists, etc. Datasets are just collections of row objects.. and rows are just collections of cell objects.. so the logic extends on how you debug it.
Was This Post Helpful? 0
  • +
  • -

#3 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Re: catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 08:04 AM

i tried this other method, but the efect is done only the first time the checkbox is checked, then the controls disappear and dont show anymore

Sub ApMICDestinationTypesDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles ApMICDestinationTypesDataGridView.CellContentClick
 
        If ApMICDestinationTypesDataGridView.Columns(e.ColumnIndex).HeaderText = "Zgjidh" Then
 
            Dim isChecked As Boolean = ApMICDestinationTypesDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
 
            If ischecked = False Then
 
                GroupTypeIDLabel.Visible = True
                GroupTypeIDComboBox.Visible = True
 
            Else
                GroupTypeIDLabel.Visible = False
                GroupTypeIDComboBox.Visible = False
 
            End If
        End If
 
    End Sub



i dont understand why, the event should be trigered each time the box is checked or unchecked, not only the first time
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9196
  • View blog
  • Posts: 34,536
  • Joined: 12-June 08

Re: catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 08:05 AM

Again.. are you putting a break point there and see what is happening?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3465
  • View blog
  • Posts: 11,754
  • Joined: 12-December 12

Re: catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 08:12 AM

I believe the following is relevant. Basically, the CellContentClick event occurs before the checkbox (or, more accurately, the data behind it) itself has been updated.

Quote

Check box cell values are intended either for storage, like any other data, or for performing bulk operations.

If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated.

If you need the new value at the time of the click,
one option is to calculate what the expected value will be based on the current value.

Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

Msdn Document:
DataGridViewCheckBoxColumn:
http://msdn.microsof...kboxcolumn.aspx
DataGridViewCheckBoxCell :
http://msdn.microsof...eckboxcell.aspx
DataGridView.CurrentCellDirtyStateChanged Event :
http://msdn.microsof...atechanged.aspx

Was This Post Helpful? 1
  • +
  • -

#6 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Re: catch the check change event in the datagridview checkbox column

Posted 15 January 2013 - 08:12 AM

yes, i have used the breakpoint there and instantly i get the error i told you. i see the value e.RowIndex takes and it is -1, thats lower than 1, thats why i get the error.
i guess the CellValueChanged event is not the right one for this case , as it is trigered since i open the form and none of the cells of the grid is changed, that's why the rowIndex is -1.

i tried the other method, but it is only trigered once
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1