<< Return to Main Index

< Return to Class Index

CGXGridCore::PasteTextFromBuffer

virtual BOOL PasteTextFromBuffer(LPCTSTR psz, DWORD size, const CGXRange& range);

psz

A pointer to the buffer which contains the data in tab-delimited form.

size

Specifies the size of the buffer.

range

Holds the currently selected range of cells in the grid. This range is used to determine the coordinates where the cells should be inserted.

Remarks

Pastes the text values from the specified buffer to the grid. The text values must be available in tab-delimited form in the buffer. Newline characters indicate that a new row should be started.

PasteTextFromBuffer is normally only called from CGXGridCore::Paste to paste data in CF_TEXT format from the clipboard, but you may also call this method to load cells from a text file (see example).

PasteTextFromBuffer will call SetControlTextRowCol for each cell to paste the text into the cell. SetControlTextRowCol will call CGXControl::SetControlText. CGXControl::SetControlText will validate the text and convert it into a value which can be stored in the grid.

Value and cell text can be different. An example is tabbed combo boxes where an entry from a choice list is displayed in the cell, and the key for that string is stored in the cell. Another example is masked edit. The text displayed in the cell contains literals (e.g., “(001)-999”). The value does not contain literals (e.g., “001999”).

SetControlText also ensures that the pasted text does not exceed the maximum text length for the cell.

If you want to validate the cells before they are pasted into the grid you have two choices:

a) Override SetControlTextRowCol if you want to do validation with the text to be pasted into the cell.

b) Override StoreStyleRowCol if you want to do validation with the value as it will be stored in the cell (see second example).

Using PasteTextFromBuffer with the Formula Engine:

SetControlText calls SetExpressionRowCol to assign the data to the cell. If you are using the formula engine and if you are pasting formula expressions beginning with an equal sign ('=') SetControlText will assign the formula to the cell.

Control-Factory Specific ->

This method has been implemented using the abstraction mechanism as discussed in the chapter "Reducing the size of your application" in the user's guide. A call to the ImplementTextDataExchange method from within the control factory class' InitializeGridComponents method will make the concrete implementation of this method available to your application.

If no concrete implementation is available this method returns FALSE. A warning will be displayed in the debug window.

END Control-Factory Specific

Example

Example one shows you how you can load data from a text file:

void CGridSampleView::OnImportTextFile( )
{
   // pop-up file-open dlg to ask for location
   CFileDialog dlgFile(
      TRUE,
      _T(".txt"),
      NULL,
      OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
      _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"));

   if (dlgFile.DoModal( ) == IDCANCEL)
      return;

   CFile textFile;

   if (!textFile.Open(dlgFile.GetFileName( ),
      CFile::modeRead))
   {
      TCHAR sz[255];
      wsprintf(sz, "File %s could not be opened!", dlgFile.GetFileName( ));
      AfxMessageBox(sz);
      return;
   }

   LPTSTR pszBuffer;
   DWORD dwSize = textFile.GetLength( );

   pszBuffer = new TCHAR[dwSize];
   textFile.Read(pszBuffer, dwSize);

   PasteTextFromBuffer(pszBuffer, dwSize, CGXRange(0,1));

   textFile.Close( );
   delete pszBuffer;
}

Example two shows you how you can override StoreStyleRowCol to validate the values before they are written to the grid:

BOOL CGridSampleView::StoreStyleRowCol(ROWCOL nRow, ROWCOL nCol, const CGXStyle* pStyle, GXModifyType mt, int nType)
{
   // Is this a try to write a value into the cell?

   if (pStyle->GetIncludeValue()
      && (mt == gxOverride || mt == gxCopy || mt == gxApplyNew))
   {
      // validate the value

      if (!IsValidValueRowCol(nRow, nCol, pStyle->GetValueRef())
         return FALSE;
   }

   // let the base-class version store the value
   base_grid::StoreStyleRowCol(nRow, nCol, pStyle, mt, nType);
}

See Also

CGXGridCore::CopyTextToFile CGXGridCore::Paste CGXGridCore::SetControlTextRowCol CGXControl::SetControlText

CGXGridCore

Class Overview | Class Members