DataRow, DataTable, DataGridView, avoid changing values in the DataRow
-
Hi everybody! I have the next code and I’m having a problem with the DataRow: 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. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005
-
Hi everybody! I have the next code and I’m having a problem with the DataRow: 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. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005
I forgot to say that I tried the following but it didn't work: private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { originalRows[e.RowIndex].CancelEdit(); originalRows[e.RowIndex].EndEdit(); }
-
Hi everybody! I have the next code and I’m having a problem with the DataRow: 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. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005
-
Hi everybody! I have the next code and I’m having a problem with the DataRow: 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. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005
You're trying to solve this 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 your
originalRows
array) will reflect that change. If all that you need to do is detect if values in the table have been modified, you can loop over every row in the table and check theRowState
property. Josh -
You're trying to solve this 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 your
originalRows
array) will reflect that change. If all that you need to do is detect if values in the table have been modified, you can loop over every row in the table and check theRowState
property. JoshThanks Led and Josh, I want to compare an specific cell not the whole row. I have the following comparation. if (!originalRows[i]["Path"].Equals(testCase["Path"])) Do you know how can I compare the cell's state? I'll try the DataSet later and I'll tell you the results. Elvia
-
Thanks Led and Josh, I want to compare an specific cell not the whole row. I have the following comparation. if (!originalRows[i]["Path"].Equals(testCase["Path"])) Do you know how can I compare the cell's state? I'll try the DataSet later and I'll tell you the results. Elvia
You can call Copy on the DataTable, instead of Select, to get a complete and separate copy of the entire table. Later, when you want to compare each value in each row, you can just compare the copied (unmodified) table to the one bound to the grid. Josh -- modified at 15:04 Friday 5th May, 2006 You might also be able to use the overload of the Select method which takes the DataViewRowState parameter. It allows you to get the original values for modified rows.
-
You can call Copy on the DataTable, instead of Select, to get a complete and separate copy of the entire table. Later, when you want to compare each value in each row, you can just compare the copied (unmodified) table to the one bound to the grid. Josh -- modified at 15:04 Friday 5th May, 2006 You might also be able to use the overload of the Select method which takes the DataViewRowState parameter. It allows you to get the original values for modified rows.
Hi Josh! Thanks a lot. I called the Copy method and did what you told me. It works as I want. I appreciate it :) Elvia