以一种形式将表格中的值添加到静态列表中以另一种形式在gridview中显示 [英] Adding values from form to static list in one form & displaying in gridview in another form

查看:60
本文介绍了以一种形式将表格中的值添加到静态列表中以另一种形式在gridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我需要一些关于winforms的帮助,我有一个针对Birds的类,它有一个静态列表,用于在一个表单中添加新鸟,然后以另一种形式在gridview中显示它们。基本上这就是我需要做的事情:



1.用户输入鸟类的名称并在下拉列表中选择一个设置。

2.当用户点击"完成鸟"时按钮,输入存储在列表中。

3.以"查看鸟类"的形式用户可以使用datagridview查看列表中存在的不同鸟。



我找不到连接列表的方法来添加用户输入form1到列表...任何能够提供帮助的人都将不胜感激。

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.

loinike

推荐答案

在Forms之间共享数据一直很棘手。一个常见的解决方法是给接收表单一个公共方法,并让它由调用表单创建,以便调用者可以保持引用。

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.

让第二个表单对列表的添加做出反应(如果使用共享列表而不是方法中的调用)是一个小问题。您可以使用.NET 3.0中的
ObservableCollection< T> 并拥有第二个form注册CollectionChanged事件。

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.

在MVVM模式下,WPF / UWP中的这类操作要容易得多。这是他们修复的问题之一。

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


这篇关于以一种形式将表格中的值添加到静态列表中以另一种形式在gridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆