Search code examples
c#winformsdevexpressxtragrid

XtraGrid has been modified


I need to know if an XtraGrid has been modified (value changed or rows added).

I can make a boolean var and change it on GridView_CellValueChanged:

void suppGridView_CellValueChanged(object sender, CellValueChangedEventArgs e) {
    isModified = true;
}

or
I can read all DataSource and check the DataRow.RowState property value (Modified or Added):

foreach (DataRow row in dataSource.Rows) {
    if (row.RowState == DataRowState.Modified || row.RowState == DataRowState.Added) 
        return true;
}

Do you know a simpler method?


Solution

  • You can know whether the data table changed in the following way:

    DataTable changes = table.GetChanges(DataRowState.Added | DataRowState.Modified);
    bool isModified = (changes != null);
    

    here table is a DataTable.
    From msdn:

    DataTable.GetChanges Method:
    Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges was called, filtered by DataRowState.

    Remark: If no rows of the desired DataRowState are found, the method returns null.