MFC Grid control with drop down lists


 Download Source code and
demo project

This is a previous version of Chris Maunders MFC Grid control
(version 1.03) with a minor bug fix and several improvements.

First a bug fix:

  • In some situations I have managed to edit cells in fixed column, so I added a line in
    OnEditCell() Event handler to prevent it:

    	if ((nRow < m_nFixedRows)||(nCol < m_nFixedCols)) return;

    And the improvements:

    • I had the need to delete all rows but without the fixed rows and all the column
      attributes (see section 3), so I add function DeleteAllRows().
    • I added an UINT array:
      m_arColType

      This array holds the Edit Type for each column. This Edit Type can take one of these
      values:

      • GVET_NOEDIT (0x0000) cells in this column are not editable.
      • GVET_EDITBOX(0x0001) cells in this column are editable in the old way (i.e. inplace Editbox is opened for the cell).
      • GVET_LISTBOX (0x0002) cells in this column are editable in the way that an inplace Listbox is opened for the user to select value for this cell.

      There are more styles (GVET_COMBOBOX, GVET_CHECKBOX) that are not implemented for now.
      (maybe someone would take the challenge to implement those styles before I do
      J).

    • To set the Column Edit type Ive changed the InsertColumn() function to set this
      attribute as well:

      	int  InsertColumn(LPCTSTR strHeading, UINT nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE, int nColumn = -1, int nType = GVET_NOEDIT);

      and I added the function:

      	BOOL SetColumnType(int nCol, int nType);

    • To let the programmer to fill the inplace Listboxes Ive added virtual function:

      	virtual void FillListItems(int nCol, LPARAM cltList);

    To use the Listbox cells attribute, you have to inherit your class from the
    CGridCtrl and override the function : FillListItems. This function is called before
    any inplace listbox is opened in order to fill its items by AddString. You can set
    the user-defined data for each listbox item. This data is attached to the Grid items
    user-defined data. (This is useful for lookup lists in the grid).

    I would like to thank all the guys who contribute articles and code to this site and especially for Zafir Anjum and all the others who keeps this site so useful. I know I gain a lot of knowledge and save a lot of time by constantly visiting here.

     

    Last updated: 17 April 1998

  • More by Author

    Get the Free Newsletter!

    Subscribe to Developer Insider for top news, trends & analysis

    Must Read