我可以控制DataGridView何时从/向其DataSource读取和写入?

时间:2015-10-02 17:55:20

标签: c# winforms datagridview .net-4.0

我绑定到List<MyCustomType>,当我在MyCustomType的属性getter上放置一个断点时,它们似乎被重复调用。导致DataGridView自动重新读取数据的原因是什么?我可以控制它吗?

其次,我注意到当我对网格中的数据进行更改时,这些数据不会立即复制到DataSource。将断点放在MyCustomType中的属性设置器上,当我在网格控件外部单击时,它们似乎只被调用。如何确保GUI中所做的更改立即应用于数据源?

1 个答案:

答案 0 :(得分:1)

从您的属性重新读取是完全正常的,这是因为渲染。当DataGridView呈现单元格时,它会从属性中读取。

支持INotifyPropertyChanged

如果要对DataGridView显示属性更改,则应实现INotifyPropertyChanged以进行双向数据绑定。这会导致对象中的更改立即在网格中可见:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class Category : INotifyPropertyChanged
{
    #region Properties
    private int _Id;
    public int Id
    {
        get
        {
            return _Id;
        }
        set
        {
            if (_Id == value)
                return;
            _Id = value;
            OnPropertyChanged();
        }
    }

    private string _Name;
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            if (_Name == value)
                return;
            _Name = value;
            OnPropertyChanged();
        }
    }
    #endregion

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}
  • 如果您使用的是.Net 4.5,请删除[CallerMemberName],并在致电OnPropertyChanged时只需传递属性名称,例如OnPropertyChanged("Name")

使用BindingList

要更改网格可见的列表,例如在向数据列表中添加新项目时,请使用BindingList<T>代替List<T>

如果您使用List<T>,则应将DataSource设置为null并再次设置为列表,以使更改对网格可见。

BindingList<Category> source = new BindingList<Category>();

private void Form_Load(object sender, EventArgs e)
{
    //Load Data to BindingList
    new List<Category>()
    {
        new Category(){Id=1, Name= "Category 1"},
        new Category(){Id=2, Name= "Category 2"},
    }.ForEach(x=>list.Add(x));

    this.categoryDataGridView.DataSource = list;
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
    //Add data to BindingList 
    //Data will be visible to grid immediately
    list.Add(new Category(){Id=3, Name= "Category 3"});
}
  • 您也可以考虑将BindingList<T>绑定到BindingSource,将网格绑定到BindingSource。使用设计师时更有意义。

使用CurrentCellDirtyStateChanged

DataGridView上的更改将自动应用于您的模型OnValidating,但正如您所提到的,您可以使用网格的CurrentCellDirtyStateChanged事件来提交对数据源的更改。

private void categoryDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (categoryDataGridView.IsCurrentCellDirty)
    {
        categoryDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}
  • 我个人不建议对所有列使用这样的技巧,例如假设您有一个字符串属性验证最小字符串长度为5,现在如何输入5个字符,然后您将收到5个验证错误消息直到你输入5个字符。

仔细选择你需要的东西。