簡體   English   中英

DataGridView.CellValueChanged無法為數據綁定的DataGridView觸發

[英]DataGridView.CellValueChanged not firing for data-bound DataGridView

盡管之前有很多類似的問題被問到,但是我沒有找到合適的答案:(。例如, 類似的問題

我的DataGridView.Cellvalues會根據數據綁定源以編程方式進行更改。 我想跟蹤某些列的總和,但是DataGridView.CellValueChanged事件僅在單元格具有焦點時才適用(不是它以編程方式更改)。 由於性能原因,我不想使用RowPrePaint Event或CellPainting事件。

有什么合適的事件或方法嗎?

我有一個類似的問題,並使用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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM