changes in DataGrid and DataTable.GetChanges()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JochenZ

    #1

    changes in DataGrid and DataTable.GetChanges()

    Hello,

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

    dataGridView.Da taSource = theTable;

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

    foreach (DataGridViewRo w row in dataGridView.Se lectedRows)
    {
    row.Cells["theColumnName1 "].Value = someCalculatedV alue1;
    row.Cells["theColumnName2 "].Value = someCalculatedV alue2;
    }

    Then the following line of code is executed:

    MessageBox.Show (theTable.GetCh anges().Rows.Co unt.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.GetCha nges()...

    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

  • rotarinn

    #2
    RE: changes in DataGrid and DataTable.GetCh anges()

    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.Tabl es[0]" with your datatable:

    CurrencyManager cm = (CurrencyManage r)BindingContex t[myDataSet.Table s[0]];
    cm.Position = -1;

    -rotarinn

    "JochenZ" wrote:
    [color=blue]
    > Hello,
    >
    > I have a DataGrid(View) and a DataTable. The DataTable is displayed in the
    > DataGridView:
    >
    > dataGridView.Da taSource = theTable;
    >
    > The user is allowed to select some rows and then the following
    > snippet updates some values in the selected rows:
    >
    > foreach (DataGridViewRo w row in dataGridView.Se lectedRows)
    > {
    > row.Cells["theColumnName1 "].Value = someCalculatedV alue1;
    > row.Cells["theColumnName2 "].Value = someCalculatedV alue2;
    > }
    >
    > Then the following line of code is executed:
    >
    > MessageBox.Show (theTable.GetCh anges().Rows.Co unt.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.GetCha nges()...
    >
    > 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
    >[/color]

    Comment

    • JochenZ

      #3
      RE: changes in DataGrid and DataTable.GetCh anges()

      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" wrote:
      [color=blue]
      > 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.Tabl es[0]" with your datatable:
      >
      > CurrencyManager cm = (CurrencyManage r)BindingContex t[myDataSet.Table s[0]];
      > cm.Position = -1;
      >
      > -rotarinn
      >
      > "JochenZ" wrote:
      >[color=green]
      > > Hello,
      > >
      > > I have a DataGrid(View) and a DataTable. The DataTable is displayed in the
      > > DataGridView:
      > >
      > > dataGridView.Da taSource = theTable;
      > >
      > > The user is allowed to select some rows and then the following
      > > snippet updates some values in the selected rows:
      > >
      > > foreach (DataGridViewRo w row in dataGridView.Se lectedRows)
      > > {
      > > row.Cells["theColumnName1 "].Value = someCalculatedV alue1;
      > > row.Cells["theColumnName2 "].Value = someCalculatedV alue2;
      > > }
      > >
      > > Then the following line of code is executed:
      > >
      > > MessageBox.Show (theTable.GetCh anges().Rows.Co unt.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.GetCha nges()...
      > >
      > > 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
      > >[/color][/color]

      Comment

      Working...