none
Change row value in DataGridview in other form and raise event RRS feed

  • Question

  • Hi all

    I have a datagridview in a form and I can  add , remove a modify rows in another form

    If I add or remove rows in the other form I can handle the dgv.RowsRemoved or dgv.RowsAdded in the main form and do something ( i.e. compute a total for numeric column in dgv)

    The question is:

    Which Event should I Handle if I modify a row (in the other forms not a directly a cell in the dgv) ?

    The Event DGV.CellValueChanged is not raised in this case

    Friday, August 3, 2018 4:00 PM

Answers

  • According to your image, you are modifying the selected row.  Therefore you should handle the CurrentItemChanged event on the BindingSource that the DataGridView is bound to, not a DataGridView event.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Tuesday, August 7, 2018 8:11 AM
    Friday, August 3, 2018 8:29 PM
    Moderator

All replies

  • I add the image of the two form 

    Friday, August 3, 2018 4:06 PM
  • The following example uses a Delegate to push data from child form to parent form. Here I use a class but it could be any type you desire.

    Public Module Module1
        Public Delegate Sub AddItemDelegate(ByVal item As Sample)
    End Module
    
    Public Class Sample
        Public Property FirstName As String
        Public Property LastName As String
        Public Overrides Function ToString() As String
            Return $"{FirstName} {LastName}"
        End Function
    End Class
    

    Main form

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            f.AddItemCallback = New AddItemDelegate(AddressOf AddItemCallbackFn)
            f.ShowDialog()
        End Sub
        Private Sub AddItemCallbackFn(ByVal pItem As Sample)
            MessageBox.Show(pItem.ToString)
        End Sub
    End Class
    
    

    Child form

    Public Class Form2
        Public AddItemCallback As AddItemDelegate
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            AddItemCallback(New Sample With {.FirstName = "Karen", .LastName = "Payne"})
        End Sub
    End Class



    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

    Friday, August 3, 2018 6:26 PM
    Moderator
  • According to your image, you are modifying the selected row.  Therefore you should handle the CurrentItemChanged event on the BindingSource that the DataGridView is bound to, not a DataGridView event.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Tuesday, August 7, 2018 8:11 AM
    Friday, August 3, 2018 8:29 PM
    Moderator
  • Hi Reed

    It works, even if in the BS.CurrentChanged is raised several times (one  for every column in the row)

    Is there any way to raise the event once ?


    • Edited by Claudio111 Monday, August 6, 2018 5:56 AM error
    Saturday, August 4, 2018 7:04 AM