One button/event to handle multiple datagrids on form

I have a application where I have several different datagrids on one form.  I would like to use ONE set of buttons/events to handle ALL datagrids depending on which grid is the "active grid", instead of duplicating buttons for each one.  Is there a way to pass  an "active grid" object, or something similar so that the same single button event is used for all datagrids on the form?
Who is Participating?
As soon as you press a button, the active grid will lose focus, so the "active" grid will be come inactive.  You could have a private variable that keeps track of which grid is the active one, and is updated upon every grid's .GotFocus or .LostFocus event.

    Private dgActive As DataGrid

    Private Sub DataGrid_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.GotFocus, DataGrid2.GotFocus
        dgActive = CType(sender, DataGrid)
    End Sub

    Private Sub DataGrid_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.LostFocus, DataGrid2.LostFocus
        dgActive = Nothing
    End Sub

Then in your button events, you would just have to use reference dgActive as the datagrid to perform the operations on.
mgb1000Author Commented:
Thank you!
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.

All Courses

From novice to tech pro — start learning today.