<< Return to Main Index

< Return to Class Index

CGXBrowserGrid::OnFlushRecord

virtual BOOL OnFlushRecord(ROWCOL nRow, CString* ps = NULL);?
throw( CException );

nRow

Specifies the row id.

ps

Specifies a pointer to a CString where the method should store a warning text if the operation did fail or throw an exception. If ps is NULL, Update will not catch any exception.

Return Value

Nonzero if successful; otherwise 0.

Remarks

This method writes dirty fields back to the data source and prepares the data source at record level before changes are written to it with OnFlushCellValue.

This action can raise exceptions. Sometimes it is necessary for the method to catch the exception, sometimes it is necessary that the exception fall through. This behavior is determined through the ps parameter.

If ps is NULL, OnFlushRecord will let exceptions fall through. If ps is a pointer to a CString, exceptions will be caught, and ps will be initialized with the text describing the exception.

This method will call OnFlushCellValue for any dirty cell in the current record buffer.

You should override this method if you need to prepare your data source at record-level before changes are written to it with OnFlushCellValue. For example, CGXDaoGrid will either call pRecordset->AddNew or pRecordset->Edit before calling the base class version of OnFlushRecord and after that calls Update (see attached example). Another typical scenario could be that you simply have to seek to a specific record in the data source.

You may also override OnFlushRecord if you want to handle additional exceptions and set the appropriate warning text by retrieving the information from the exception object.

When overriding this method, it is strongly recommended that you dereference the record index associated with a row by calling the GetRecordFromRow method.

Example

The following two samples demonstrate the two scenarios described above.

a) CGXDaoGrid::OnFlushRecord either calls pRecordset->AddNew or pRecordset->Edit before calling the base class version of OnFlushRecord and after that calls Update.

BOOL CGXDaoGrid::OnFlushRecord(ROWCOL nRow, CString* ps)
{
   CGXBrowseParam* pBrowseData = GetBrowseParam();

   try
   {
      COleVariant varBookmarkCurrent;
      if (m_pRecordset->CanBookmark() && !m_pRecordset->IsEOF() && !m_pRecordset->IsBOF())
         varBookmarkCurrent = m_pRecordset->GetBookmark();

      // Sandwich CDaoRecordset into Edit/AddNew and Update
      if (pBrowseData->m_nEditMode == addnew)
         m_pRecordset->AddNew();
      else
      {
         VERIFY(MoveTo(nRow));
         m_pRecordset->Edit();
      }

      // Force writing dirty cells to CDaoRecordset
      // Base class version contains the necessary functionality
      // to check all fields and call OnFlushCellValue

      CGXBrowserGrid::OnFlushRecord(nRow, NULL /* force throwing an exception */);

      m_pRecordset->Update();

      if (varBookmarkCurrent)
         m_pRecordset->SetBookmark(varBookmarkCurrent);
   }
   catch (CDaoException* e)
   {
      GridWnd()->SendMessage(WM_CANCELMODE, 0, 0);

      // Reset cursor
      if (!m_pRecordset->IsEOF() && !m_pRecordset->IsBOF())
         m_pRecordset->SetFieldDirty(NULL, FALSE);

      // ps is a valid CString if we shall catch the exception
      if (ps)
      {
         TRACE0("CGXDaoGrid::OnFlushRecord failed!\n");

         *ps = e->m_pErrorInfo->m_strDescription;
         e->Delete();
      }
      // else
         // let exception fall through

      // failed, ...
      return FALSE;
   }
   catch (CException* e)
   {
      // ps is a valid CString if we shall catch the exception
      if (ps)
      {
         TRACE0("CGXDaoGrid::OnFlushRecord failed!\n");

         ps->Empty();
         e->Delete();
      }
      // else
         // let exception fall through

      // failed, ...
      return FALSE;
   }

   return TRUE;
}

b) CDbfBrowserView::OnFlushRecord (from the DbfBrows sample) simply seeks to a specific record in the data source and, after calling the base class version of OnFlushRecord, flushes the changes to the current record.

BOOL CDbfBrowserView::OnFlushRecord(ROWCOL nRow, CString* ps)
{
   ASSERT(nRow <= LONG_MAX);
   long nRecord = GetRecordFromRow(nRow);

   if (!GetDocument()->m_dbfile.Seek(nRecord))
      return FALSE;

   // Force writing dirty cells to DBase file
   // Base class version contains the necessary functionality
   // to check all fields and call OnFlushCellValue

   TRY
   {
      CGXBrowserGrid::OnFlushRecord(nRow, NULL /* force throwing exception */);
   }
   CATCH(CException, e)
   {
      // ps is a valid CString if we shall catch the exception
      if (ps)
      {
         TRACE0("CGXBrowserGrid::OnFlushRecord failed!\n");

         // OnFlushCellValue did call SetWarningText(s);
         // this warning text will then be displayed.
         ps->Empty();
      }
      else
         // let exception fall through
         THROW_LAST();

      // failed, ...
      return FALSE;
   }
   END_CATCH

   // flush changes to file
   GetDocument()->m_dbfile.Flush();

   SetModifiedFlag();

   return TRUE;
}

See Also

CGXBrowserGrid::OnFlushCellValue CGXBrowserGrid::GetRecordFromRow CGXGridCore::SetWarningText

CGXBrowserGrid

Class Overview | Class Members