none
Ignoring changes in a single column RRS feed

  • Question

  • Hi,

    I have a DataTable within my Dataset. One of the columns in this dataset is used to sort information with regard to the use of a combobox, and I need to set the values in this column in a manual fashion. This column is not saved to my database.

    If values change in this column, but in none of my other columns, then I want the dataset's HasChanges to return false. How do I set all the cells in a column as "not changed"?

    Thanks,

    Barry
    Tuesday, April 22, 2008 8:37 AM

All replies

  • There's no built-in mechanism for doing this for a specific column; I think you'd need to implement a method like this:

     

    Code Snippet

    public void SetMyColumn(DataRow row, string value)

    {

    bool hasChanges = (row.RowState != DataRowState.Unchanged);

    row["MyColumn"] = value;

    if (!hasChanges)

    {

    row.AcceptChanges();

    }

    }

     

    You can also do this on a row by row basis before you call your table adapter's Update method, e.g.:

     

    Code Snippet

    public void PrepareTable(DataTable table)

    {

    foreach (DataRow row in table.Rows)

    {

    if (row.RowState = DataRowState.Modified)

    {

    bool modified = false;

    foreach (DataColumn col in row.Table.Columns)

    {

    if (col.ColumnName != "MyColumn")

    {

    if row(col, DataRowVersion.Current).ToString() != row(col, DataRowVersion.Original.ToString())

    {

    modified = true;

    break;

    }

    }

    }

    if (!modified)

    {

    row.AcceptChanges()

    }

    }

    }

    }

     

     

     

    Tuesday, April 22, 2008 7:03 PM