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



  • 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?

      Reply
    • 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

      Reply
    • 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

      Reply
    • 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?

      Reply
    • Implement checkbox

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

      Originally posted by: Mike Wonder

      How can I implement checkboxes?

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

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

      Originally posted by: ahed bader

      hi
      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
      thenks;
      ahed bader
      gaza palistine

      Reply
    • 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?

      Reply
    • 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);
      }
      else
      {
      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?

      Thanks!
      Jean-Claude

      Reply
    • 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.
      

      Reply
    • Diable sorting at Drop Down Listbox..

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

      Originally posted by: P. H. Kim

      Hi,

      Thanks to Motty Cohen.

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

      at InPlaceList.cpp

      CHANGE

      DWORD dwStyle2 = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL|
      CBS_DROPDOWNLIST|CBS_AUTOHSCROLL;

      TO

      DWORD dwStyle2 = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL;

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

      Have a nice day~

      Reply
    • Loading, Please Wait ...

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

    Top White Papers and Webcasts

    • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

    • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds