CGXGridCore::OnValidateCell

virtual BOOL OnValidateCell(ROWCOL nRow, ROWCOL nCol);?

nRow

Specifies the row id.

nCol

Specifies the column id.

Return Value

TRUE if the specified cell’s data is valid; FALSE if invalid.

Remarks

Overridable method which is called to check if the specified cell’s data is valid. The method is called by CGXControl::OnValidate. Return TRUE if the cell’s data is valid.

You have two options for displaying a message box with warning text when the data are invalid:

a) Call SetWarningText and specify the text. The grid-component will later call the overridable method DisplayWarningText to display the message box.

b) Call AfxMessageBox directy from within OnValidateCell, which lets you gather user feedback. For example, you may ask the user if he wants to ignore the data or accept it. Note: When you want to display a message box yourself, you have to follow some rules (See the attached example. Most important is to call m_bWarningTextDisplayed = TRUE).

You can either override OnValidateCell for all controls or subclass a specific control and override the OnValidate method to check if the specified cell’s data are valid.

You can use CGXControl::IsKindOf to determine the control’s class and compute the return value depending on the control’s runtime-type information.

Note

Don’t call GetValueRowCol to determine or SetValueRange to change the value of the current cell, because the value has not been transferred to the grid yet at the time OnValidate is called. GetValueRowCol will only return the value before the cell was changed.

To determine the text of the current cell you should call

   CString s;
   GetCurrentCellControl( )->GetCurrentText(s);

To change the text of the current cell you should call

   GetCurrentCellControl( )->SetCurrentText(sNewString);

Example

The first example illustrates how to override the OnValidateCell and call SetWarningText.

// validate input
BOOL CDerGrid::OnValidateCell(ROWCOL nRow, ROWCOL nCol)
{
   CString s;
   CGXControl* pControl = GetControl(nRow, nCol);
   pControl->GetCurrentText(s);
   if (!s.IsEmpty( ))
   {
      if (_ttoi(s) < 1 || _ttoi(s) > 100)
      {
         SetWarningText (_T("Please enter a value between 1 and 100!"));
         return FALSE;
      }
      return TRUE;
   }
   return CGXGridView::OnValidateCell(nRow, nCol);
}

The second example displays a message box and also demonstrates how to correct the value or reset the cell to its previous value. Please note that we check the m_bLockActivateGrid before we display a message box. This avoids a message box being displayed when the grid loses focus because a user hit the “Cancel” key in a dialog.

BOOL CSample2GridWnd::OnValidateCell(ROWCOL nRow, ROWCOL nCol)
{
   CString s;
   // retrieve text from current cell
   CGXControl* pControl = GetControl(nRow, nCol);
   pControl->GetCurrentText(s);
   if (_ttol(s) < 0 || _ttol(s) > 100)
   {
      TCHAR sz[255];
      wsprintf(sz, _T("Please enter a value between 0 and 100!"));
      ScrollCellInView(nRow, nCol);
      // Display message box
      if (m_bLockActivateGrid)
      {
         AfxMessageBox(sz);
         // m_bWarningTextDisplayed is looked up in OnLButtonHitRowCol
         // if you set this TRUE, OnLButtonHitRowCol will cancel any further mouse processing
         m_bWarningTextDisplayed = TRUE;
         // If you want the current cell to be undone, call
         // TransferCurrentCell(FALSE);
         // This is only a sample. You might also display a message box
         // and ask the user if he wants to retry or ignore the value or
         // you could also call pControl->SetCurrentText() to change
         // the value.
         // pControl->SetCurrentText("22");
      }
      // else do nothing, the focus will go back to the current cell
      return FALSE;
   }
   return TRUE;
}

The third example displays a message box and lets the user choose Yes, No or Cancel.

BOOL CGridSample9View::OnValidateCell(ROWCOL nRow, ROWCOL nCol)
{
   CString s;
   // retrieve text from current cell
   CGXControl* pControl = GetControl(nRow, nCol);
   pControl->GetCurrentText(s);
   // Display message box
   if (m_bLockActivateGrid)
   {
      int result = AfxMessageBox(s, MB_YESNOCANCEL);
      // m_bWarningTextDisplayed is looked up in OnLButtonHitRowCol
      // if you set this TRUE, OnLButtonHitRowCol will cancel any further mouse processing
      m_bWarningTextDisplayed = TRUE;
      // If Yes then move to next cell.......
      if(result == IDYES)
         return TRUE;
      // If No then clear the current text and return to the current cell
      else if(result == IDNO)
      {
         TransferCurrentCell(FALSE);
         return FALSE;
      }
      // if cancel, then return to the current cell for further editing.
      else if(result = IDCANCEL)
         return FALSE;
   }
   return TRUE;
}

See Also

 CGXGridCore::GetControl  CGXControl::OnValidate  CGXControl::IsKindOf  CGXGridCore::SetWarningText  CGXGridCore::DisplayWarningText  CGXControl::GetCurrentText

CGXGridCore

 Class Overview |  Class Members