Editable Flex Grid (without an Edit control)

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:
       m_grid.GoDown();
       return TRUE;
     
       case VK_LEFT : 
       m_grid.GoLeft();
       return TRUE;
     
       case VK_RIGHT: 
       m_grid.GoRight();
       return TRUE;
      }
     }		
     
     return CFormView::PreTranslateMessage(pMsg);
    }
    
    void CFlexGridView::OnInitialUpdate()
    {
     CFormView::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 () );

 //MAGIC NUMBERS ????
 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) 
  GetDeviceConstants();

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

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

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

Downloads

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


Comments

  • Editable Flexgrid

    Posted by jaiguru on 11/05/2008 04:46am

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

    Reply
  • Magic Numbers

    Posted by dhn on 10/22/2005 06:44pm

    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.

    Reply
  • PleaseHelpme..emergency!!

    Posted by Legacy on 10/17/2003 12: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...

    Reply
  • Edit Box Control

    Posted by Legacy on 04/04/2003 12: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.

    Thanks

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

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

    Originally posted by: Eric

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

    Reply
  • How to add any charater in FixedColumn

    Posted by Legacy on 10/24/2002 12: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.

    thanks.

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

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

    Originally posted by: win

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

    Reply
  • Help in getting values from each cell !!!

    Posted by Legacy on 02/18/2002 12: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?

    Thanx,
    Nitin

    Reply
  • How to insert a blank row in the grid

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

    Originally posted by: CandyAppleDan

    Hi,

    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.

    Reply
  • MultiSelection with a flexgrid?

    Posted by Legacy on 07/30/2001 12: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?

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Live Event Date: November 18, 2014 @ 11:00 a.m. EST As you embrace the hybrid world of on-premise and cloud applications, often accessed via mobile devices, you now have to be concerned that cybercriminals have yet another vehicle to attack your business. In fact, the average cost of cybercrime has increased over 10% in the last year, and this applies to businesses of all sizes. Attend this webinar to hear David Monahan, Security Research Director at EMA, and Dana Epp, recognized security luminary from …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds