<< Return to Main Index

< Return to Class Index

CGXBrowserGrid::OnFlushCellValue

virtual void OnFlushCellValue(ROWCOL nRow, ROWCOL nCol, LPCTSTR pszChangedValue);?
throw( CException );

nRow

Specifies the row id.

nCol

Specifies the column id.

pszChangedValue

 pointer to a string which contains the changed value for the cell. Pointer must not be NULL.

Remarks

OnFlushCellValue is called from within CGXBrowserGrid::OnFlushRecord for all values changed in the current buffer.

You should override this method to store changed values in your data source. If you cannot write the passed value to the data source, you may throw an exception and specify the text to be displayed in the grid with SetWarningText (see the attached example).

The default implementation writes data into the CGXData object.

When overriding this method, it is strongly recommended that you dereference the record and field index associated with a row or column by calling the GetRecordFromRow or GetFieldFromCol methods.

OnFlushCellValue is not called when the user changes the current cell in the current record (unlike StoreStyleRowCol). It is called when the user wants to move to a new record and therefore validation in OnFlushCellValue happens only at record level.

Example

This sample demonstrates how you can convert values into a data source-specific format (e.g., date values into a region-independent format), validate the cell contents, and throw an exception if data are invalid.

void CDbfBrowserView::OnFlushCellValue(ROWCOL nRow, ROWCOL nCol, LPCTSTR pszChangedValue)
{
   // Unused:
   nRow;

   ASSERT(pszChangedValue != NULL);

   // Cell value
   CField* fld = GetField(nCol);

   // Date
   //

   if (fld->type == 'D' && *pszValue)
   {
      // Convert displayable text into plain date values
      // when record gets flushed. If date format is invalid,
      // an exception will be thrown.
      char szDbfDate[128];

      TIMESTAMP_STRUCT ts;
      if (GXParseDateTime(&ts, pszChangedValue, gxDEFAULT))
      {
         wsprintf(szDbfDate, _T("%04d%2d%2d"), ts.year, ts.month, ts.day);
         // Cell value
         GetDocument()->m_dbfile.SetValue(GetFieldFromCol(nCol), pszChangedValue);
      }
      else
      {
         // set warning text so that a message box is displayed
         // and throw an exception
         CString s;
         AfxFormatString1(s, GX_IDM_DATEINVALID, pszChangedValue);
         SetWarningText(s);
         AfxThrowNotSupportedException();
      }
   }
   else if (fld->type == 'N')
   {  
      TCHAR szFormat[10], szValue[128];
      wsprintf(szFormat, _T("%%%d.%df"), fld->len, fld->decimals);
                                       
      BOOL bIsDigit = FALSE;                                       
      for (LPCTSTR p = pszChangedValue; *p; p++)
      {            
         if (_istdigit(*p) || _istpunct(*p))
            bIsDigit = TRUE;
         else if (!_istspace(*p))
         {
            // set warning text so that a message box is displayed
            // and throw an exception
            CString s;
            AfxFormatString1(s, IDM_NUMBERINVALID, pszChangedValue);
            SetWarningText(s);
            AfxThrowNotSupportedException();
         }  
      }

      if (bIsDigit)
         _stprintf(szValue, szFormat, _gxttof(pszChangedValue));
      else
         _tcscpy(szValue, _T(""));
      GetDocument()->m_dbfile.SetValue(GetFieldFromCol(nCol), szValue);
   }
   else
   {
      TCHAR szFormat[10], szValue[128];
      wsprintf(szFormat, _T("%%-%ds"), fld->len);
      wsprintf(szValue, szFormat, pszChangedValue);
      GetDocument()->m_dbfile.SetValue(GetFieldFromCol(nCol), szValue);
   }
}

See Also

CGXBrowserGrid::OnFlushRecord CGXBrowserGrid::GetRecordFromRow CGXBrowserGrid::GetFieldFromCol CGXGridCore::SetWarningText CGXData

CGXBrowserGrid

Class Overview | Class Members