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:
      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

  • Comments

    • How to add a Date/Time Edit in one column

      Posted by Legacy on 07/16/2003 12:00am

      Originally posted by: LoveisDark

      I am new to c++, i like the code , but i want to add a date/Time edit column, how can i do? when dblclick , popup a windows Date/Time Dialog,where to find help?

    • How to sort contents in the grid columns

      Posted by Legacy on 05/10/2003 12:00am

      Originally posted by: Prashant

      How to sort contents in the grid columns

    • how to merge cells?

      Posted by Legacy on 07/16/2002 12:00am

      Originally posted by: HTV

      I don't know how to merge cells like VideoSoft Flex Grid

    • crash after press enter

      Posted by Legacy on 04/30/2002 12:00am

      Originally posted by: xxx

      crash after press enter after edit cell?why?

    • Implement checkbox

      Posted by Legacy on 04/03/2001 12:00am

      Originally posted by: Mike Wonder

      How can I implement checkboxes?

    • why (OnFillList )does not take the row number??

      Posted by Legacy on 02/18/2001 12:00am

      Originally posted by: ahed bader

      i want to make all one of the cell (in the list column)has its one data and take it from table
      so... i need to now the row no. of the cell
      so how
      ahed bader
      gaza palistine

    • Why List Column be hide by the Custom Control

      Posted by Legacy on 11/22/1999 12:00am

      Originally posted by: NoTearsPIG

      When i want to choice the last Item of the list column,
      i found that it has been hided by the custom control.

      Who can help me?

    • Drop Down with new Version (4.10.99)?

      Posted by Legacy on 09/16/1999 12:00am

      Originally posted by: Jean-Claude Demosthenidy

      I would like to use your solution (column type) with the new version of the grid (4.10.99) - Have you done this?
      I tried including the changes in InPlaceList, but I am missing something.

      void CInPlaceList::EndSelect()
      CString csItemText;
      DWORD dwUserData = 0;
      int nItem = GetCurSel();

      if ( nItem != LB_ERR )
      dwUserData = GetItemData(nItem);
      GetText( nItem, csItemText);
      csItemText = m_sInitText;

      // Send Notification to parent (this was originally designed for listviews,
      // so we use listview structures and messages)
      // Send Notification to parent
      GV_DISPINFO dispinfo;

      dispinfo.hdr.hwndFrom = GetSafeHwnd();
      dispinfo.hdr.idFrom = GetDlgCtrlID();
      dispinfo.hdr.code = GVN_ENDLABELEDIT;

      dispinfo.item.mask = LVIF_TEXT|LVIF_PARAM;
      dispinfo.item.row = m_nRow;
      dispinfo.item.col = m_nCol;
      dispinfo.item.szText = csItemText;
      dispinfo.item.lParam = (LPARAM)dwUserData;

      CWnd* pOwner = GetOwner();
      if (pOwner)
      pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo );

      // Close this window (PostNcDestroy will delete this)
      PostMessage(WM_CLOSE, 0, 0);

      But the parent never gets notified... So the grid is never updated. Any idea?


    • Having a per-cell type would be incredible.

      Posted by Legacy on 03/03/1999 12:00am

      Originally posted by: Desperately seeking fix

      Column type is cool, having a per-cell type would be great.

    • Diable sorting at Drop Down Listbox..

      Posted by Legacy on 02/19/1999 12:00am

      Originally posted by: P. H. Kim


      Thanks to Motty Cohen.

      If anyone doesn't want sorting at drop down listbox, Just remove some codes.

      at InPlaceList.cpp





      or simply make a function to change listbox property as you like.

      Have a nice day~

    • Loading, Please Wait ...

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

    • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date