How can I programmatically append a record to the data source?

The following sample code shows you how you can programmatically append a record to your data source.

Example:

   // Append a row to the grid
   if (CanAppend())
   {
      // Add a row
      ROWCOL nAppendRow = AddNew();
      ASSERT(nAppendRow != GX_INVALID);
      // Lock updating the grid
      BOOL bLock = LockUpdate(TRUE);
      // Change cells in new row
      SetValueRange(CGXRange(nAppendRow, 1), "1");
      SetValueRange(CGXRange(nAppendRow, 2), "2");
      // Unlock painting
      LockUpdate(bLock);
      // Flush pending changes, Update will also redraw
      // the row.
      Update();
   }

Please note that this code depends only on CGXBrowserGrid-functionality and thus makes appending rows independent from your specific data source.  You can use the same code for appending a row to a DAO recordset, ODBC recordset or any other external data source.

Attention!

You have to change fields, so that the record will be set dirty. Otherwise, the call to Update() will undo the AddNew() command.

The following sample shows an alternative that allows you to add a record directly to a DAO recordset and bypass the grid. This should work similar for ODBC.

   CDaoRecordset* prs = OnGetRecordset();
   prs->AddNew();
   prs->SetFieldValue(0, "2");
   prs->Update();
   // Now, call OnAddedNewRecord so that the grid
   // gets informed that a record has been added.
   OnAddedNewRecord();
   // Scroll to the last row
   MoveCurrentCell(GX_BOTTOM);
   MoveCurrentCell(GX_LEFT);