Using a Combobox to edit cells in the Grid Control

gridctrl with combo example image Download sample project.

Environment: VC++ 5.0 (SP3) NT 4.0 (SP4) - CGridCtrl version 1.12

Since I posted my MFC grid control I've had a lot of requests asking how to use other methods of editing cells in the grid. I've created a demo project that shows how to replace the default editing with a dropdown list of choices. This is achieved by deriving a new class from CGridCtrl and overridding CGridCtrl::CreateInPlaceEditControl.

The In-Place list that I use to edit the cells instead of the in-place edit control is not the greatest - but it does demonstrate how to replace the default editting.

Thanks go to Roelf Werkman for his work in extending the CInPlaceList to allow the CBS_DROPDOWN and CBS_SIMPLE styles to be used.

The first step is to derive a new class from CGridCtrl - I call it CComboGridCtrl - and override CreateInPlaceEditControl. There are a number of things to be aware of when you override this function. CreateInPlaceEditControl receives the row and column to be edited, the dimensions and location of the in-place edit control, the style of control to create, the original text in the cell plus the initial character that caused editing to commence (or VK_LBUTTON if the mouse was clicked on the current cell).

  • Your editing control must self-delete when it loses input focus.
  • Your editing object should handle mouse keys in a way that allows the user to navigate between cells while editing. If the control gets an arrow key for instance, it should cancel editing and return the last key it encountered back to the grid via the GVN_ENDLABELEDIT notification message.

    Note that the grid control (as of version 1.08) accepts Ctrl+<arrows> for navigation, so if your in-place edit control needs to use the arrows, you can always reserve Ctrl+<arrows> to move to other cells.

To pass the necessary data back to the parent grid once editing has been completed, use the following code snippet in your in-place edit control:

	// Send Notification to parent
	GV_DISPINFO dispinfo;

	dispinfo.hdr.hwndFrom = GetSafeHwnd();          // Handle of window
	dispinfo.hdr.idFrom   = GetDlgCtrlID();         // control ID
	dispinfo.hdr.code     = GVN_ENDLABELEDIT;       // Message
 
	dispinfo.item.mask    = LVIF_TEXT|LVIF_PARAM;   // Not used in default mode
	dispinfo.item.row     = m_nRow;                 // Row - previously stored
	dispinfo.item.col     = m_nCol;                 // column - previously stored
	dispinfo.item.szText  = str;                    // New text
	dispinfo.item.lParam  = (LPARAM) m_nLastChar;   // Last character encountered

I pass the character that initiated editing to the edit control itself so that it can deal with keys appropriately. For instance in the default edit control, if the edit control is passed a normal character or arrow key as the "initiating" key, then it will cease editing when it encounters an arrow key (to allow navigation using the keyboard). If the edit control is passed VK_LBUTTON (meaning editing was initiated by a mouse click) then arrow keys will not cause the editing to cease.

Once you have an edit control ready to be used, override CreateInPlaceEditControl and use your own edit control instead of the default in-place edit control.

void CComboGridCtrl::CreateInPlaceEditControl(CRect& rect, DWORD dwStyle, UINT nID,
                                              int nRow, int nCol,
                                              LPCTSTR szText, int nChar)
{
	CStringArray Items;
	Items.Add("Choice 1");
	Items.Add("Choice 2");
	Items.Add("Choice 3");
	Items.Add("Choice 4");
	Items.Add("Choice 5");

	// InPlaceList and auto-deletes itself
	new CInPlaceList(this, rect,
                     //CBS_DROPDOWNLIST,    // Uncomment for dropdown list style
                     CBS_DROPDOWN,          // Uncomment for dropdown style
                     //CBS_SIMPLE,          // Uncomment for simple style
                     nID,                   // ID of control being created
                     nRow, nCol, 
                     Items, szText, nChar);
}

The sample project includes the source code for the CInPlaceList control presented above.

Last Updated: Aug 11, 1998.



Comments

  • How to handling Combo's CBN_SELCHANGE Message?

    Posted by Junaid Ali on 06/30/2004 12:37am

    SIR... can i handle the CBN_SELCHANGE message of the combo.. if yes, how to do that....plz guide me through...

    Reply
  • How to communicate with the DLP Board(Hardware)

    Posted by Legacy on 02/23/2004 12:00am

    Originally posted by: sowmya

    Dear Sir,
    I'm Doing my project on VC++ to interact with the hardware signals.I need to develop the front end GUI to interact with the user and he has to enter some range of voltage & time in ms
    When he click OK button the voltage the user has entered should set in the hardware (we can see this through oscilloscope)Thus we have to interact this by microcontroller & USB .Please help me out.I'll appreciate u'r participation in advance.I need to do this within 5'o clock today.
    Best Regards,
    Sowmya.

    Reply
  • How can I insert a CDateTimeCtrl in a cell?

    Posted by Legacy on 02/09/2004 12:00am

    Originally posted by: Puiu

    How can I insert a CDateTimeCtrl in a cell?

    Reply
  • GridControl

    Posted by Legacy on 03/21/2003 12:00am

    Originally posted by: Naveen S.G.

    i want to interact with the grid control which is created from ur class i.e. CGridCtrl.
    
    for eg: In some application there is grid control which was created using CGridCtrl.
    from my application i want to interact with the grid control which is in that application.
    i can get the handle using FindWindowEx method.
    i have created object of CGridCtrl in my application.
    i have attached the handle of grid control,what i got using FindWindowEx.
    but when i tried to extract the information of grid control like Totalnumber of rows using CGridCtrl methods, 0 is returning.
    i am using the same logic what i did using CTreeCtrl in some project.There it is working for Treecontrol.
    here to access the information from your GridControl, what steps i need to follow.
    plz reply me asap. its required for my project.
    plz.............................

    plz reply me to
    " naveen.gangadharappa@honeywell.com " .


    Reply
  • How to send EndEdit message to Dialog in combobox ...

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

    Originally posted by: Mr,Lee

    I putted GridCtrl in Modaless Dialog.
    After Editing in combobox, when I select another cell,
    EndEdit() in CInplaceList is called.
    But dialog class don't know it.

    Then, where can I receive message for notifying EndEdit in Dialog?

    Help me plz.
    Thanks.

    Reply
  • How to build a grid in Win32 and C++, not MFC

    Posted by Legacy on 06/15/2002 12:00am

    Originally posted by: Tran Toan

    Dear sir,
    I need a sample about grid in Win32 built in C++, not MFC.
    That grid can be resized, edited
    can you show me how to build or where to download source code?

    Thanks!

    TToan

    Reply
  • How to handling Combo's CBN_SELCHANGE Message?

    Posted by Legacy on 05/13/2002 12:00am

    Originally posted by: SangHeon Kwon

    Generally When CComboBox's list is changed handle the CBN_SELCHANGE.

    In Grid's combo,
    how to handle Combo's CBN_SELCHANGE ?

    Show me the way plz....

    Reply
  • Bug found -Assertion

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

    Originally posted by: Vivek Kharche

    I am using combo grid control and found a bug.
    
    I set some Column count say 10 and row count 2
    I select some cell and a edit box is created in the cell.
    say in cell (1,8)
    Now i set the column count to 2 keeping same row count.
    It gives assertion.I tried by explicitely setting the FocusCell to -1,-1,but it doesn'e help.

    If anybody knows the solution,mail me at vskharche@yahoo.com
    Vivek

    Reply
  • How to change the background Colors?

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

    Originally posted by: yongwoon shin

    How to change the background Colors?

    Reply
  • How to specify non-editable column

    Posted by Legacy on 05/24/2001 12:00am

    Originally posted by: new user

    How to specify non-editable column in this grid control?

    Also, how to change the color of the grid dynamically?

    Pl help.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds