JharPaat
JharPaat

Reputation: 341

DataGridView.CellValueChanged not firing for data-bound DataGridView

Although there are many similar questions were asked before, but i didn't find suitable answer :(. e.g. Similar question

My DataGridView.Cellvalues changes as per databounded source programatically. I would like to track sum of certain columns, but DataGridView.CellValueChanged event is only applicable when cell has focus(not it case it changed programatically). I dont want to use RowPrePaint Event , or CellPainting events because of performance reason.

Is there any suitable events or approach?

Upvotes: 1

Views: 2808

Answers (1)

madz
madz

Reputation: 1002

I had a similar problem and solved it using the DataBindingComplete event.

For working with certain columns only, this should perform well for you as I believe it's only called once after data binding operations are complete.

You will need to do your own iteration through the grid as required as the DataGridViewBindingCompleteEventArgs obviously does not have the RowIndex and ColumnIndex properties, but you can do something like this:

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
    }
}

Upvotes: 2

Related Questions