updating DataTable using DataGrid

From: JochenZ (JochenZ_at_discussions.microsoft.com)
Date: 09/25/04


Date: Sat, 25 Sep 2004 02:05:02 -0700

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.

I think the changes in the last selected row of the DataGridView are in a
way 'not committed', probably by 'not leaving the row'. So I also tried to
run the foreach loop twice in succession. That would make sure the last
selected row 'had the same treatment' as the others. It didn't help though...

Could anyone help me out please?

Thanks a lot!

Jochen



Relevant Pages

  • RE: DataGridView
    ... select multiple rows in a DataGridView but the current row can only be one ... If you'd like to just change the selected row, ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms)
  • RE: DataGridView - Set current row
    ... DataGridview. ... The row that is highlighted is the selected row of the ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Manipulating a bound datagridview
    ... example) remove the currently selected row from this ... Should I go through the datagridview, ... DataRowView drv = dgvAttachments.CurrentRow.DataBoundItem; ...
    (microsoft.public.dotnet.framework.windowsforms)
  • datagridview programmatic row navigation
    ... How do I update my datagridview with a record that, once added, is sorted ... moves to the specific row I need and give that row focus in the dgv so it ... displays as the selected row. ...
    (microsoft.public.dotnet.languages.vb.controls)