Skip to content

DataRow, DataTable, DataGridView, avoid changing values in the DataRow

Elvia Gonzalez

Asked May 5, 2006, 10:21 AM EDT1 answer
Share
LinkedInX

Hi everybody!

 

I have the next code and I’m having a problem with the DataRow:

 

Code
DataTable dt = new DataTable();
dt.Load(GetReader());
dataGridView.DataSource = dt;
DataRow[]originalRows = dt.Select();

 

The problem is that when I change a cell value in the datagridview the change is also set in the originalRows values. I’d like to avoid this, since I want to use the original values of the datatable to compare them later to see whether there ware changes in my datagridview.

 

I tried the following but it didn't work:

 

Code
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
originalRows[e.RowIndex].CancelEdit();
originalRows[e.RowIndex].EndEdit();
}

 

 

Thanks in advance,

 

Elvia

 

PS: Windows application in Microsoft Visual C# 2005

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1

    Hi!

     

    I was trying to solve the problem the wrong way. The Select method of the DataTable returns references to the rows in the table, not clones. When a value in a row is changed via the grid, the corresponding row in the table (and, hence, an element in my originalRows array) will reflect that change.

     

    So I call Copy on the DataTable, instead of Select, to get a complete and separate copy of the entire table. Later, I compare each value in each row with the copied (unmodified) table to the one bound to the grid.

     

    The problem is solved thanks to somebody else who helped me. I wanted to share this with you, so if you have the problem later.

     

    Best Regards,

     

    Elvia

    +1