我将ListCollectionView绑定到BindingSource,而BindingSource又绑定到DataGridView(winforms)。但每当将新对象添加到ListCollectionView时,BindingSource都不会自动更新。我需要将其设为NULL并再次重新绑定。
//Binding to Datagrid
bindingProvider.DataSource = this.GetController.ProvidersView;
this.dgvProviders.DataSource = bindingProvider;
之后在添加按钮中单击。
//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 bcoz中,它没有反映在DataGridView中
www.techknackblogs.com
答案 0 :(得分:0)
确保您的基础集合(您曾用于创建CollectionView
)实现INotifyCollectionChanged
。
例如,不要使用List<T>
,而是使用ObservableCollection<T>
或BindingList<T>
。
这允许更改集合(添加元素)以传播到CollectionView
。