DataGridView.cellvaluechange不触发数据绑定的DataGridView

本文关键字:DataGridView 数据绑定 cellvaluechange | 更新日期: 2023-09-27 18:17:56

虽然之前有很多类似的问题被问过,但是我没有找到合适的答案:)例:类似的问题

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

是否有合适的事件或方法?

DataGridView.cellvaluechange不触发数据绑定的DataGridView

我有一个类似的问题,并使用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
    }
}