Home > Blockchain >  Event Value changed in DataTable bound to DataGridView
Event Value changed in DataTable bound to DataGridView

Time:05-23

I have DataTables in a DataSet in a data base bound to a DataGridView. I'd like to show a status if the data changed. So I used the CellValueChanged event of the DataGridView as trigger. In the event routine I check for changes in the DataSet with DataSet.HasChanges if there was really a change. But that is to early, I asume the DataGridView didn't updated the DataTable at that time. From the 2nd change on it works. Is there another event which is fired after updating the data table?

CodePudding user response:

Using DataTable.ColumnChanged Event, make a change, press TAB or ENTER to get original and proposed value. In the following example we first subscribe to the DataTable ColumnChanged event

someDataTable.ColumnChanged  = ColumnChanged;

Then, in this case show values to Visual Studio output window

public void ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
    Console.WriteLine($"       Column name {e.Column.ColumnName}");
    Console.WriteLine($"    Original value [{e.Row[e.Column.ColumnName, DataRowVersion.Original]}]");
    Console.WriteLine($"    Propose value [{e.ProposedValue}]");
}

Or be selective

public void ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
    if (
        e.Row.RowState == DataRowState.Deleted || 
        e.Row.RowState == DataRowState.Detached || 
        e.Row.RowState == DataRowState.Added) 
        return;

    Console.WriteLine($"       Column name {e.Column.ColumnName}");
    Console.WriteLine($"    Original value [{e.Row[e.Column.ColumnName, DataRowVersion.Original]}]");
    Console.WriteLine($"    Propose value [{e.ProposedValue}]");

}
  • Related