的WinForm控件绑定到一个List< T>问题 [英] WinForm Controls binding to a List<T> problem

查看:115
本文介绍了的WinForm控件绑定到一个List< T>问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的存储数据的列表中,我尝试它的项目绑定到网格,列表框,文本框等,但不能让它工作得很好。这里是我的代码

i have a List for storing my data, i try to bind its items to grid,listbox,textbox etc. but cant make it work well. Here is my code

class Survey
{
   public int Id { get; set; }
   public string Desc{ get; set; }
   public List<string> Choices { get; set; }
}



.

List<Survey> _surveyList = GetList();
BindingSource _bindingSourceSurveys = new BindingSource { DataSource=_surveyList};
dataGridView1.DataSource = _bindingSourceSurveys;
txtDesc.DataBindings.Add("Text", _bindingSourceSurveys, "Desc",false,DataSourceUpdateMode.OnPropertyChanged,string.Empty);
lstChoices.DataBindings.Add("DataSource" , _bindingSourceSurveys,"Choices" ,false,DataSourceUpdateMode.OnPropertyChanged, string.Empty);

现在我可以看到的数据网格的,则selectedItem(上的数据网格)项目上的textBox说明属性值,也可以改变从文字框说明PROPERT值。

Now i can see items on dataGrid, selectedItem(on dataGrid) Desc property value on textBox and also can change Desc propert value from textBox.

如果我添加一个新的选择,我将selectedItem列表选择像

If i add a new choice to my selectedItem List Choices like that

(_bindingSourceSurveys.Current as Survey).Choices.Add("NewChoice");

请注意:我不能添加到ListBox.Items,因为它给例外,因为我做结合的数据源这种控制。

Note: I cant add to ListBox.Items because it give Exception because of i make binding to DataSource of this control.

ListBox中不显示新的项目,如果我选择从数据网格中的不同项目,回头我可以看到新增加的选择。

ListBox dont show the new item, if i select a different item from dataGrid and turn back i can see the new added choice.

这里有什么问题吗?也是该代码是确定的,它的我第一次使用这个绑定设施。

What is the problem here? Also Is that code is OK, its my first time to use this binding facilities.

推荐答案

您的数据源不引发事件时元素得到改变。一个简单的办法就是改变从清单的BindingList数据源。

Your data source does not raise events when elements gets changed. An easy fix is to change the datasource from List to BindingList.

这篇关于的WinForm控件绑定到一个List&LT; T&GT;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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