Using the Microsoft Flexgrid in Visual C++

Environment: Controls, Visual C++

Introduction

When I had to use the flexgrid for the first time, I was frustrated to find out that no proper documentation existed on the Net. The ones available were too complicated for me to understand. And the ones that were easy were in VB. This article is to help persons who are going to use the flexgrid in their applications using VC++. Also, take a look at the Warning note at the end of this article.

Implementation

  1. First, include the msflexgrid control in the project. To do that:
    1. In the VC++ IDE Menu, go to Project->Add To Project->Components and Controls menu.
    2. Then, in the dialog box that appears, select Registered ActiveX Controls. There, you can see a control named "Microsoft Hierarchical FlexGrid Control...".
    3. Select that and use the Insert button to add it to your project. You can now see the FlexGrid control in the resource editor's CONTROLS toolbar.
    4. Add it to your dialog as you usually add a control.
  2. Then, set the properties as you want them to be. Right-click on the grid and select the Properties option. Change the settings there as per your requirements.
  3. Add a member variable for that grid; for example, m_Grid. The sample given below is an example. Use it as a guideline to make your application.
  4. It's better to clear the flexgrid before you do any operations on it. To do that, use:
  5. m_Grid.Clear();
  6. To add records to the Grid, use something like this:
  7. CString strName,strRemarks;
    
    m_nCount = 0;
    
    // Clear and refresh the grid
    m_Grid.Clear();
    m_Grid.Refresh();
    
    // Get the value for strName from the database here
    // Get the strRemarks from the database here
    
    m_nCols = m_Grid.GetCols();
    m_Grid.SetTextArray(0, "Name ");        // First Column
    m_Grid.SetTextArray(1, "Remarks " );    // Second Column
    
    m_nCount++;
    
    // Fill First Column
    m_Grid.SetTextArray(  m_nCols * m_nCount + 0,strName );
    // Fill Second Column
    m_Grid.SetTextArray(  m_nCols * m_nCount + 1, strRemarks );
    
    // Redraw the grid
    m_Grid.SetRedraw(TRUE);
    m_Grid.Refresh();
    
  8. To retrieve data from the Grid when the user double-clicks on a record, use the class wizard to add a double-click message handler for the grid. Assuming that you have named the function OnDblClickGrid, then:

    void YourDialog::OnDblClickGrid()
    {
        // Get the current row and column
        int nRow = m_Grid.GetRow();
        int nCol = m_Grid.GetCol();
    
        CString strName,strRemarks;
    
        // Get data from the First Column
        strName    =  m_Grid.GetTextMatrix(nRow,nCol+0);
        // Get data from the Second Column
        strRemarks =  m_Grid.GetTextMatrix(nRow,nCol+1);
    }
    
  9. To move to the previous record, do this:
  10. void YourDialog::OnBtnPrevious()
    {
        m_Grid.SetRedraw(FALSE);
    
        // Get the current selection
    
        int NextRow = m_Grid.GetRowSel();
    
        // If the position is at the last record, return
        if(NextRow <= 1)
        {
            return;
        }
        else
        {
            long BackColor[2],FontColor[2];
    
            int Column;
    
            // The BackColor and the FontColor variables are
            // manipulated because we want an effect to be given
            // to the previous record. Here, we are merely
            // changing the color of the selected row to give
            // it that effect.
    
            BackColor[0] = 0x00FFFFFF;
            BackColor[1] = 0x00FFFFB0;
    
            FontColor[0] = 0x00400000;
            FontColor[1] = 0x000000FF;
    
            for(Column = 1; Column < m_Grid.GetCols(); Column++)
            {
                m_Grid.SetCol(Column);
                m_Grid.SetCellBackColor(BackColor[0]);
                m_Grid.SetCellForeColor(FontColor[0]);
            }
    
    
            m_Grid.SetRow(--NextRow);
    
            for(Column = 1; Column < m_Grid.GetCols(); Column++)
            {
                m_Grid.SetCol(Column);
                m_Grid.SetCellBackColor(BackColor[1]);
                m_Grid.SetCellForeColor(FontColor[1]);
            }
    
            m_Grid.Refresh();
            m_Grid.SetRedraw(TRUE);
        }
    }
    
  11. To go to the Next record, do the reverse of the code above. Instead of m_Grid.SetRow(--NextRow), it would be m_Grid.SetRow(++NextRow).

Warning

Whenever you use a flexgrid in your application, you have to be twice as careful. Be sure to take regular backups. Sometimes, you might note that your compilation time is longer than usual. It would be slower and slower with each build. If you feel this is happeing, check the size of your .rc file. If its size is abnormal—I can't specify a size because it depends on your project and its resource contents—delete the flexgrid from your application, Close Visual Studio, and copy the .rc file from your backup. Open your project again. That should solve your problem. I'm not sure why this happens. Maybe it's a bug in the flexgrid that corrupts or damages it. But from your side, be careful.

Conclusion

The flexgrid is a very easy control to use. But whenever you use it, repeat the mantra "make backups of your .rc file regularly" until it becomes a habit. That's it. All luck and have a great time.



Comments

  • msflexgrid redefinition

    Posted by LucianoLiu on 04/25/2006 09:53am

    Hello,
      When I use a dialog box with only one msflexgrid control,everyting is OK.But when there are two dialog boxes in my project,and there is a grid in each dialog,for example:
    
    in dialog 1: CDlgProWZ.h
    
    //{{AFX_INCLUDES()
    #include "msflexgrid.h"
    //}}AFX_INCLUDES
    
    in dialog 2: CDlgProJS.h
    
    //{{AFX_INCLUDES()
    #include "msflexgrid.h"
    //}}AFX_INCLUDES
    
    When compiling,it shows an error:
    error C2011: 'CMSFlexGrid' : 'class' type redefinition
    
    How to solve this problem?
    Thanks a lot!

    Reply
  • How to add sub-bands to a band in a Hierarchical flexgrid control ?

    Posted by DineshR on 11/29/2004 06:07am

    How do I add values to different bands ?

    Reply
  • How can insert checkbox in FlexGrid?

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

    Originally posted by: Cho Yoon Sik

    Hi

    Please Help Me!!

    I'm coding database program.

    I got a problem.

    How can insert checkbox in each cell?

    Reply
  • how to edit like chinese char

    Posted by Legacy on 01/16/2004 12:00am

    Originally posted by: williamgu

    I look the web at microsoft http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q196/8/33.ASP&NoWebContent=1#appliesto,
    
    the way to edit can not put in chinese word when the cell is empty , I must set focus with my mouse before I putinto chinese words.

    Reply
  • How can I get the cols' and rows' numbers when user selected multiple cells?

    Posted by Legacy on 12/30/2003 12:00am

    Originally posted by: Wonhyuk Choi

    As you know, with GetCol() and GetRow(), you can get the column number and row number of the selected cell.

    But I couldn't find the way to get cols' and rows' numbers when user selected multiple cells by dragging mouse cursor.

    Help me please. :)

    Reply
  • MS FlexGrid && fonts

    Posted by Legacy on 12/22/2003 12:00am

    Originally posted by: Vladimir

    I would appreciate if somebody explain me how I can use
    putref_FontFixed(LPDISPATCH newValue) method to set FontFixed property. I put this component in mdichild form by coding so that I can not use resource editor to set properties in designe time.

    Vladimir

    Reply
  • help me

    Posted by Legacy on 12/14/2003 12:00am

    Originally posted by: kalyanaraman

    I want to move the contents of flexgrid to another flexgrid in Visual basic. How can I do that

    Reply
  • HowTO Install FlexGrid if it is not present in system ?

    Posted by Legacy on 11/12/2003 12:00am

    Originally posted by: v02d02

    I have a problem with useing FlexGrid on other machines =(

    Reply
  • Scrolling beyond end of cell

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

    Originally posted by: Chris

    Is there a way to allow the user to scroll within a 
    
    

    cell if the data in the cell is longer than the width

    of the cell itself?

    -Chris

    Reply
  • Why? said Debug Assertion Failior

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

    Originally posted by: Khayyam

    Why it always said that debug assertion failior, 
    
    and when i start debugging it stops on this thing
    > Assert(m_pCtrlSite != NULL)
    // Not an OLE object, not yet atleast

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

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

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds