<< Return to Main Index

< Return to Class Index

CGXDaoGrid::AttachForeignTable

void AttachForeignTable(ROWCOL nCol, CDaoRecordset* pSet, UINT nKeyCol, UINT nDispCol, BOOL bDispAllCols, BOOL bDisplayHeader);

nCol

Specifies the column id in your grid that contains key values.

pSet

Pointer to the recordset with the foreign table (or query).

nKeyCol

The zero-based column id that specifies the column in the foreign recordset with the key values that match the values in the grid column.

nDispCol

The zero-based column id that specifies the column in the foreign recordset to be used for displaying the cell text in the grid.

bDispAllCols

TRUE if you want to display all columns of the foreign recordset in the drop-down list; FALSE if you only want to drop down the text that will be displayed in the cell (column nDispCol).

bDisplayHeader

TRUE if the field names of the columns in the foreign recordset shall be displayed as column headers in the drop-down list; FALSE if no headers shall be displayed.

Remarks

This method lets you attach a table with a foreign key to a column.

The method will open the given recordset, loop through all rows and build up the choice list. This choice list and the GX_IDS_CTRL_TABBED_COMBOBOX cell type will be assigned to the column base style, so that all cells in the column can drop down the foreign table. See CGXTabbedComboBox for a discussion about tabbed combo boxes.

For example, you could attach a foreign table to the column and display a descriptive text for a key (e.g., student name instead of student id).

Note

Once you have attached a foreign table to a column with AttachForeignTable outside changes in this foreign table will not update the choice list. In order to update the column with new changes you have to call AttachForeignTable again.

Example:

      // attach the table to the column
      CDaoRecordset set(&GetDocument()->m_database);

      CString strSQL = _T("Select StudentId, Name From Student");
      UINT nKeyCol = 0;  // Student Id
      UINT nDisplayCole = 1; // Display the name

      // Open the foreign recordset (we have to open
      // it on our own because we want to specify the SQL query
      // dynamically with the call to Open)
      set.m_bCheckCacheForDirtyFields = FALSE;

      try
      {
         set.Open(AFX_DAO_USE_DEFAULT_TYPE, strSQL);
      }
      catch (CDaoException* e)
      {
         // Tell them the reason it failed to open
         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
         e->Delete();
         return;
      }

      ResetCurrentCell();

      AttachForeignTable(nCol, &set,
         nKeyCol, nDispCol, TRUE /* Show all cols */,  TRUE /* Show header */);

      ResizeColWidthsToFit(CGXRange(GetHeaderRows()+1, nCol));
   }

ee AlsoCGXTabbedComboBox

CGXDaoGrid

Class Overview | Class Members