none
datagridview : reversing cell value RRS feed

  • Question

  • Good Morning

    I am getting a : System.StackOverflowException Error

    I can modify any other column but 7

    I am trying to reverse any change in value on column 7 when column 11 is equal to 0

    Private Sub dg_main_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles  dg_main.CellValueChanged
            If Me.dg_main.RowCount = 0 Then Exit Sub
            With Me.dg_main
                If e.ColumnIndex = 7 Then
                   If .Rows(e.RowIndex).Cells(11).Value = 0 Then
                        .Rows(e.RowIndex).Cells(7).Value = 0
                    End If
                End If
            End With
    End Sub

    Please help

    John

    Friday, October 12, 2018 3:57 PM

Answers

  • Put a RemoveHandler before making changes, make the change, use AddHandler to restore the event handler. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bre-x Friday, October 12, 2018 5:40 PM
    Friday, October 12, 2018 5:30 PM
    Moderator

All replies

  • Hi

    The reason for the exception is that you are trying to change a cell value INSIDE the event handler that handles cell value changes - so, recursively calls itself over and over until something goes BOOM!


    Regards Les, Livingston, Scotland

    Friday, October 12, 2018 4:25 PM
  • Hello, 

    > I can modify any other column but 7

    I didn't think so. 

    You modifying cell inside CellValueChanged handler - this cause a recursion-like re-call.

    -----

    by the way in 1001 time - do not do calculations in form. Have your Data part and do anything related with calculation there. 


    Sincerely, Highly skilled coding monkey.

    Friday, October 12, 2018 4:31 PM
  • Thank you both for you answer.

    BTW the datagridview is unbound

    The back end DB is ms access I changed the field to allow Nulls

    If I changed my code and it works when change it back to null

               If e.ColumnIndex = 7 Then
                    If .Rows(e.RowIndex).Cells(11).Value = 0 Then
                        .Rows(e.RowIndex).Cells(7).Value = DBNull.Value
                    Else
                        If (.Rows(e.RowIndex).Cells(4).Value - .Rows(e.RowIndex).Cells(5).Value) = 0 Then
                            .Rows(e.RowIndex).Cells(7).Value = DBNull.Value
                        End If
                    End If
                End If

    if the problem is recursion how is this working?

    john


    • Edited by Bre-x Friday, October 12, 2018 4:40 PM
    Friday, October 12, 2018 4:40 PM
  • Why it is unbound is your business, but you are making life very difficult for yourself. If you were using a datatable you could make use of an IIF function within a DataColumn expression and you would not require any additiaonl code or event handler 

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Friday, October 12, 2018 4:52 PM
    Friday, October 12, 2018 4:51 PM
  • Hi

    Put a breakpoint in the event handler and single step from there.


    Regards Les, Livingston, Scotland

    Friday, October 12, 2018 4:52 PM
  • Put a RemoveHandler before making changes, make the change, use AddHandler to restore the event handler. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bre-x Friday, October 12, 2018 5:40 PM
    Friday, October 12, 2018 5:30 PM
    Moderator
  • wooohooo

    :)

    Private Sub dg_main_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dg_main.CellValueChanged
            If Me.dg_main.RowCount = 0 Then Exit Sub
            RemoveHandler dg_main.CellValueChanged, AddressOf dg_main_CellValueChanged

            With Me.dg_main
                If e.ColumnIndex = 7 Then
                    If .Rows(e.RowIndex).Cells(11).Value = 0 Then
                        .Rows(e.RowIndex).Cells(7).Value = DBNull.Value
                    Else
                        If (.Rows(e.RowIndex).Cells(4).Value - .Rows(e.RowIndex).Cells(6).Value) = 0 Then
                            .Rows(e.RowIndex).Cells(7).Value = 0 'DBNull.Value
                        End If
                    End If
                End If
            End With
            AddHandler dg_main.CellValueChanged, AddressOf dg_main_CellValueChanged
        End Sub

    Friday, October 12, 2018 5:41 PM