DataGridView.CellValueChanged没有为数据绑定的DataGridView触发

时间:2015-10-16 12:17:30

标签: c# winforms data-binding datagridview

虽然之前有过许多类似的问题,但我没有找到合适的答案:(例如Similar question

我的DataGridView.Cellvalues以编程方式根据数据源进行更改。我想跟踪某些列的总和,但DataGridView.CellValueChanged事件仅在单元格具有焦点时适用(不是它以编程方式更改的情况)。由于性能原因,我不想使用RowPrePaint事件或CellPainting事件。

有没有合适的活动或方法?

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题并使用DataBindingComplete事件解决了它。

对于仅使用某些列,这应该对您有用,因为我相信它只在数据绑定操作完成后调用一次。

您需要根据需要在网格中进行自己的迭代,因为DataGridViewBindingCompleteEventArgs显然没有RowIndex和ColumnIndex属性,但您可以这样做:

private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    var senderGrid = (DataGridView)sender;

    foreach (DataGridViewRow row in senderGrid.Rows)
    {
        // Determine the bound data object for the current row
        var currentRowObj = row.DataBoundItem;

        // Access the data in a particular cell on this row
        var cellToTotal = row.Cells["Column1"];

        etc
    }
}