changes in DataGrid and DataTable.GetChanges()


G

Guest

Hello,

I have a DataGrid(View) and a DataTable. The DataTable is displayed in the
DataGridView:

dataGridView.DataSource = theTable;

The user is allowed to select some rows and then the following
snippet updates some values in the selected rows:

foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
row.Cells["theColumnName1"].Value = someCalculatedValue1;
row.Cells["theColumnName2"].Value = someCalculatedValue2;
}

Then the following line of code is executed:

MessageBox.Show(theTable.GetChanges().Rows.Count.ToString());

It turns out that the number of rows returned by GetChanges() is always one
lower than the number of selected rows in the DataGridView. When n rows are
selected, the foreach loop is executed for n rows, n rows change in the
DataGridView, but only n-1 rows are returned by theTable.GetChanges()...

Updating complete rows at once in the DataGridView using row.SetValues(...)
doesn't make a difference. The last selected row, though changed in the
DataGridView, is never actually changed in the DataTable.

Could anyone help me out please?

Thanks a lot!

Jochen
 
Ad

Advertisements

G

Guest

Hi,
I haven't worked a lot with datagrids but this is my best guess.

The datagrid doesn't seem to detect changes to a row until you actually move
out of the cell that you just changed. That may be the reason that the
getChanges() method never includes the last row changed.

Try adding this code to your project, before you call the getChanges method,
replacing the "myDataSet.Tables[0]" with your datatable:

CurrencyManager cm = (CurrencyManager)BindingContext[myDataSet.Tables[0]];
cm.Position = -1;

-rotarinn
 
Ad

Advertisements

G

Guest

I had the same guess, and so I tried to run the foreach loop twice in
succession. This way, it would surely have left the last selected row in the
same way as it had left the others. But that didn't help...

Jochen

rotarinn said:
Hi,
I haven't worked a lot with datagrids but this is my best guess.

The datagrid doesn't seem to detect changes to a row until you actually move
out of the cell that you just changed. That may be the reason that the
getChanges() method never includes the last row changed.

Try adding this code to your project, before you call the getChanges method,
replacing the "myDataSet.Tables[0]" with your datatable:

CurrencyManager cm = (CurrencyManager)BindingContext[myDataSet.Tables[0]];
cm.Position = -1;

-rotarinn

JochenZ said:
Hello,

I have a DataGrid(View) and a DataTable. The DataTable is displayed in the
DataGridView:

dataGridView.DataSource = theTable;

The user is allowed to select some rows and then the following
snippet updates some values in the selected rows:

foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
row.Cells["theColumnName1"].Value = someCalculatedValue1;
row.Cells["theColumnName2"].Value = someCalculatedValue2;
}

Then the following line of code is executed:

MessageBox.Show(theTable.GetChanges().Rows.Count.ToString());

It turns out that the number of rows returned by GetChanges() is always one
lower than the number of selected rows in the DataGridView. When n rows are
selected, the foreach loop is executed for n rows, n rows change in the
DataGridView, but only n-1 rows are returned by theTable.GetChanges()...

Updating complete rows at once in the DataGridView using row.SetValues(...)
doesn't make a difference. The last selected row, though changed in the
DataGridView, is never actually changed in the DataTable.

Could anyone help me out please?

Thanks a lot!

Jochen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top