One button/event to handle multiple datagrids on form

Posted on 2006-06-01
Last Modified: 2010-04-23
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?
Question by:mgb1000
    LVL 17

    Accepted Solution

    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.

    Author Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now