Adding values from form to static list in one form & displaying in gridview in another form RRS feed

  • Question

  • Hello! I need some help regarding winforms, I have a class for Birds which is to have a static list for adding new birds in one form and then displaying them in a gridview in another form. Basically this is what I need to do:

    1.The user inputs a name of the type of bird and chooses a setting in a drop down list.
    2. When the user clicks the "Finish Bird" button, the input is stored in the list.
    3. In the form of "View Birds" the user can see the different birds that exist in the list using a datagridview.

    I just can't find a way to connect the list to add the user input from form1 to the list...Anyone who might be able to help out here would be greatly appreciated.


    • Moved by CoolDadTx Wednesday, March 8, 2017 3:57 PM Winforms related
    Sunday, March 5, 2017 11:17 AM

All replies

  • Sharing data between Forms has always been Tricky. A common workaround is just to give the receiving form a public method, and have it be created by the calling form so the caller can maintain a reference.

    I would advise against a static class of any kind. This has been a common mistake from the early days of programming.
    At the very least use a normal instantiated class, that is assigned to a static Variable. It at least avoids the worst pitfall of a single static data provider class.

    Having the 2nd Form react to additions to the list (if you use a shared list rather then the call in approach) is a minor Problem. You could use a ObservableCollection<T> from .NET 3.0 and have the 2nd form register the CollectionChanged Events.

    Operations like this are significantly easier in WPF/UWP under the MVVM pattern. It was one of the problems they fixed.

    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Sunday, March 5, 2017 11:22 PM