【发布时间】:2014-06-06 17:29:50
【问题描述】:
当我第一次开始从事这个项目时,我从未使用过任何 .NET,更不用说 C# 或 WinForms,所以如果我所做的任何事情让你泪流满面,请告诉我最佳做法是这种设置。
我正在开发一个调度系统。我将事件保存在数据库中,然后将这些事件从 Web 服务获取到主列表中以供应用程序使用。这个Event类包含一个活动列表(List):
[Global class]
public List<Event> Events = Service.GetEvents();
我有一个显示事件的 DataGridView 和一个显示事件活动的第二个 Datagridview。我还有一个“保存”按钮,只有在事件发生任何变化时才会启用该按钮。这是我的设置方式:
[EventsView form]
BindingSource eventsBindingSource = new BindingSource();
BindingSource activitiesBindingSource = new BindingSource();
List<Event> events = new List<Event>(Global.Events.ToList());
eventsBindingSource.DataSource = events;
activitiesBindingSource.DataSource = ((Event)eventsBindingSource.Current).Legs //updated when clicked on new row.
eventsDataGridView.DataSource = eventsBindingSource;
activitiesDataGridView.DataSource = activitiesBindingSource;
我设置了它,以便当 avtivitiesDataGridView 上的 CellValueChanged 时,它会检查活动BindingSource.Current,在原始 Global.Events 列表中查找该事件的活动,如果它不同,它将启用“保存”按钮。
但是,当我检查值是否不同时,你瞧,Global.Events 列表已经更新,即使 BindingSource 设置为事件列表,它是该列表的副本。
BindingSource 是否有它,如果列表是副本,它也会更新原始列表?
我不希望它自动更新并保存它的原因是我可以在用户单击保存时更新Web服务。
我还尝试了以下方法:
- 将(复制)事件列表设为 BindingList
- 从全局列表中逐一复制事件
- 绕过 BindingSource 并将 DGV 的数据源设置为 List
当我将 Global.Events 设置为一个不同的类时,这曾经可以工作,我使用它的唯一目的是在 Web 服务和应用程序之间进行传输(因为实体框架会有无法通过的集合),但是因为我已更改该列表以包含已转换的 Event 对象,它能够自动更新它。
是否有类似于 DataBindings 中的 UpdateMode,除非我告诉它,否则我可以将其设置为不更新?或者我只是在这一切都错了?
感谢您的帮助,如果您需要更多详细信息,请告诉我。
【问题讨论】:
标签: c# winforms list datagridview bindingsource