我将ListCollectionView绑定到BindingSource,后者又绑定到DataGridView (winforms)。但是,无论何时向ListCollectionView添加新对象,BindingSource都不会自动更新。我需要将它设为空,然后再次重新绑定。
//Binding to Datagrid
bindingProvider.DataSource = this.GetController.ProvidersView;
this.dgvProviders.DataSource = bindingProvider;之后在Add Button Click。
//Adds new object in ProvidersView Collection.
this.GetController.AddEditProvider();
this.bindingProvider.DataSource = null;
this.bindingProvider.DataSource = this.GetController.ProvidersView;有人能告诉我刷新Bindingsource的简单方法吗?
以下是示例代码
BindingList<DemoCustomer> lstCust = new BindingList<DemoCustomer>();
BindingListCollectionView view;
private void Form1_Load(object sender, EventArgs e)
{
lstCust.Add(DemoCustomer.CreateNewCustomer());
lstCust.Add(DemoCustomer.CreateNewCustomer());
lstCust.Add(DemoCustomer.CreateNewCustomer());
lstCust.Add(DemoCustomer.CreateNewCustomer());
view = new BindingListCollectionView(lstCust);
bindingSource1.DataSource = view;
dataGridView1.DataSource = bindingSource1;
}
private void button1_Click(object sender, EventArgs e)
{
this.lstCust.Add(DemoCustomer.CreateNewCustomer());
bindingSource1.EndEdit();
this.bindingSource1.ResetBindings(false);
//(bindingSource1.DataSource as BindingListCollectionView).NeedsRefresh
dataGridView1.Refresh();
}
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(312)555-0100";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged("CustomerName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged("PhoneNumber");
}
}
}
}请让我知道我的代码有什么问题。每当我添加任何新项目时,它都不会在BindingSource中反映出来,因为它不会在DataGridView中反映出来
www.techknackblogs.com
发布于 2013-12-05 01:32:47
确保您的底层集合(用于创建CollectionView)实现INotifyCollectionChanged。
例如,不使用List<T>,而使用ObservableCollection或BindingList。
这允许对集合所做的更改(添加元素)传播到CollectionView。
https://stackoverflow.com/questions/20380530
复制相似问题