DataGridViewCheckBoxColumn:如何在属性更改而不是验证时更新绑定数据源 [英] DataGridViewCheckBoxColumn: how to update bound DataSource on property changed instead of on validation

查看:25
本文介绍了DataGridViewCheckBoxColumn:如何在属性更改而不是验证时更新绑定数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经绑定了一个 BindingList 作为 DataGridView 的数据源;TSource 属性之一绑定到 DataGridViewCheckBoxColumn,但数据源不是在单击复选框时更新,而是在复选框本身失去焦点时更新.

I've got a BindingList binded as the data source of a DataGridView; one of the TSource properties is binded to a DataGridViewCheckBoxColumn, but the data source is updated not when a click on the checkbox occurs, but when the focus on the checkbox itself is lost.

我知道当 DataSourceUpdateMode 为OnValidation"而不是OnPropertyChanged"时,标准 WindowsForms 绑定上会发生类似的事情,但是如何使用 DataGridViewCheckBoxColumn 获得相同的结果?

I know that something similar happens on a standard WindowsForms binding when the DataSourceUpdateMode is "OnValidation" instead of "OnPropertyChanged", but how can I have the same results with a DataGridViewCheckBoxColumn?

列定义如下:

            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            column.DataPropertyName = "MyProperty";
            column.HeaderText = "Title";

            dataGridView.Columns.Add(column);

推荐答案

您可以通过处理 DataGridViewCurrentCellDirtyStateChanged 事件来做到这一点.

You can do this by handling the CurrentCellDirtyStateChanged event of the DataGridView.

void dataGridView1_CurrentCellDirtyStateChanged(object sender,EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

这篇关于DataGridViewCheckBoxColumn:如何在属性更改而不是验证时更新绑定数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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