Virtual Developer Workshop: Containerized Development with Docker

Editable Flex Grid: How to edit Flex grid?

Indeed there is a demo project in MSDN for VB. But there is no example for VC. There is no one to one conversion of the source from VB to VC. Even if you convert the VB project to VC, you are still left with some of the magic numbers to map correctly edit box to the grid cell. For my different views I have to always twick those magic numbers to get edit box in sync with the flexgrid cells. At the end of the article I shown the edit box approach also. If some body knows a way to eliminate those magic numbers, please let me know.

I used a different approach than using an Edit control. With this approach, I am trapping the keys and manipulating them which gives user a feeling as he is editing the grid cell directly.

This approach is very easy to use as I put all the functionality in a single class called CGrid.

Using the CGrid Class

  1. You simply need to include this class' header file in your project and declare a CGrid object.
  2. CGrid m_grid;
  3. Define an HWND hGrid variable in your view class.
  4. From the Flex Grid click and key press events just call the m_grid equivalent functions as follows:
  5. void CFlexGridView::OnClickMsflexgrid1() 
     m_grid.OnClickGrid ();
    void CFlexGridView::OnKeyPressMsflexgrid1(short FAR* KeyAscii) 
     m_grid.OnKeyPressGrid ( KeyAscii);	
    BOOL CFlexGridView::PreTranslateMessage(MSG* pMsg) 
     if (pMsg->message == WM_KEYDOWN  
     && (pMsg->hwnd == hGrid  ) ) {
      switch (pMsg->wParam ) {
       case VK_UP : 
       m_grid.GoUp ();
       return TRUE;
       case VK_DOWN:
       return TRUE;
       case VK_LEFT : 
       return TRUE;
       case VK_RIGHT: 
       return TRUE;
     return CFormView::PreTranslateMessage(pMsg);
    void CFlexGridView::OnInitialUpdate()
     CWnd * hwnd = GetDlgItem (IDC_MSFLEXGRID1);
     hGrid = hwnd->GetSafeHwnd ();
Thats all for making flex grid editiable . Enjoy.

Part 2

Following is the code which is a translation of the MSDN VB code example to Visual C++.
void CGridEdit::MoveEditBox ()
 HDC hDC; 
 hDC = ::GetDC (NULL); 

 SetMapMode (hDC, MM_TWIPS);

 RECT rec;
 m_grid.GetWindowRect (&rec);

 int x,y,h,w;
 x =  XTwipsToPixels (rec.left) 
   + XTwipsToPixels ( m_grid.GetCellLeft () );

 y =  YTwipsToPixels (rec.top ) 
   +  YTwipsToPixels( m_grid.GetCellTop ()  );

 h = (int) XTwipsToPixels (m_grid.GetCellHeight() );
 w = (int) YTwipsToPixels(m_grid.GetCellWidth () );

 y = y + 148;    
 x = x + 27; 

 m_edit.MoveWindow  (x ,y,w,h ,TRUE); //has to be in pixels
 m_edit.BringWindowToTop ();

 m_edit.SetFocus ();	

 TransferValue (FALSE); //get the value in Editbox

Utility Functions

BOOL CGridEdit::TransferValue(BOOL ToGrid)
 CString sText;
 if (ToGrid) { //Transfer value to Grid from Edit Box
  GetDlgItemText (IDC_EDITBOX,sText);
  m_grid.SetText( sText );
 } else {
  sText = m_grid.GetText ();
  SetDlgItemText (IDC_EDITBOX,sText); 

 return TRUE;
short lpx = 0;
short lpy = 0;
// called once to get constants
void GetDeviceConstants(void)
 HDC hdc = GetDC(NULL);
 //pixel in one logical inch
 lpx = GetDeviceCaps(hdc, LOGPIXELSX); 
 lpy = GetDeviceCaps(hdc, LOGPIXELSY);

 ReleaseDC(NULL, hdc);
long XTwipsToPixels(long twips)
 if (!lpx) 

 return MulDiv(lpx, (int)twips, 1440);
 // Total pixel in Inch = Total pixel in twips/1440
long YTwipsToPixels(long twips)
 if (!lpy) 

 return MulDiv(lpy, (int)twips, 1440);
long XPixelsToTwips(long pixels)
 if (!lpx) 

 return MulDiv(pixels, 1440, lpx);
long YPixelsToTwips(long pixels)
 if (!lpx) GetDeviceConstants();
  return MulDiv(pixels, 1440, lpy);


Download demo application - 16 Kb
Download demo source - 31 Kb


  • Editable Flexgrid

    Posted by jaiguru on 11/05/2008 12:46pm

    Nice article sir. We like to share our download - Flexgrid component with source, with you to all programmers of this community. with regards, wbcsoftwarelab

  • Magic Numbers

    Posted by dhn on 10/23/2005 01:44am

    I think the magic number are because GetWindowRect gets the rectangle for the whole window, not the client area, that is in included the title area, frame etc. MoveWindow is relative to the parents client area.
    The best way to work these out these magic numbers is with calls to GetWindowInfo and AdjustWindowRectEx. From these it is possible to work out the offsetsto the client area.

  • PleaseHelpme..emergency!!

    Posted by Legacy on 10/17/2003 07:00am

    Originally posted by: shailesh Kanzariya

    Hi !  I have just gone through your article.
    It is really Impressive Article.
    Now, What i have problem.
    It is that when i put MSFlexgridControl into Propertypage then that page itself is going to be disappeared from the sheet.Please, give me the solution...

  • Edit Box Control

    Posted by Legacy on 04/04/2003 08:00am

    Originally posted by: GayRek

    I want the edit box control to accept only numbers, special characters like [, ., ], *. I want to suppress all other characters. How can I program that in VC++ environment.


  • How to display a picture in a cell of FlexGrid?

    Posted by Legacy on 11/21/2002 08:00am

    Originally posted by: Eric

    Help!How to display a bitmap, or together with text, on a cell of FlexGrid?

  • How to add any charater in FixedColumn

    Posted by Legacy on 10/24/2002 07:00am

    Originally posted by: racuca

    I added 1 FixedRow and 1 FixedColumn in MSFlexgrid Control
    And each title in FixedRow using setFormatString function.
    But, I don't know how to add each title in FixedCol.

    Please, let me know that.


  • Where did you get the msflexgrid.h & msflexgrid.cpp?

    Posted by Legacy on 08/23/2002 07:00am

    Originally posted by: win

    Did those files in your project are your own creation or they are existing somewhere?

  • Help in getting values from each cell !!!

    Posted by Legacy on 02/18/2002 08:00am

    Originally posted by: Nitin Bhatnagar

    I need to extract values contained in each cell at run time from this editable flex grid control. Can you tell me how can I retrieve value of each cell in MSFlexGrid control?


  • How to insert a blank row in the grid

    Posted by Legacy on 08/28/2001 07:00am

    Originally posted by: CandyAppleDan


    Is it possible to insert a blank row between rows. I have been attempting to do this but have not achieved a desired result.

    Many Thanks
    Dan Escott.

  • MultiSelection with a flexgrid?

    Posted by Legacy on 07/30/2001 07:00am

    Originally posted by: Carl Austin

    Is there anyway that you can return an array of all of the row numbers that are selected at the same time?

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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