问题描述:当在DataGridView中更新了数据后,BindingSource不会自动反映这些更新。
解决方法:
// 更新数据
// ...
// 重新绑定数据并刷新DataGridView
bindingSource.ResetBindings(false);
// 更新数据
// ...
// 通知BindingSource数据已更新
int index = bindingList.IndexOf(updatedItem);
if (index >= 0)
{
bindingList.ResetItem(index);
}
public class CustomDataSource : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
NotifyPropertyChanged(nameof(Name));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
// 更新数据
customDataSource.Name = "New Name";
请注意,以上解决方法适用于WinForms中的DataGridView。如果是使用其他UI框架或控件,可能需要根据具体情况进行相应的调整。