MFC Grid control with drop down lists | CodeGuru

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: […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 5, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


 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.

     

    CodeGuru Logo

    CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.