Click here to Skip to main content
13,445,901 members (42,125 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 28 Jun 2002

Using the Flexgrid in VC++

, 30 Jul 2002
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the usage of the MS-Flexgrid using MFC


When I had to use the flexgrid for the first time, I was too 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.


Step 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 add it to your project using the Insert button. You can now see the FlexGrid control in the resource editor's CONTROLS toolbar.
  4. Add it to your dialog as you add a control usually.

Step 2

Then, set the properties as you want it to be. Right click on the grid and select the Properties option. Change the settings there as per your requirements.

Step 3

Add a member variable for that grid. E.g. m_Grid. The sample given below is for an example. Use it as a guideline to make your application.

Step 4

Its better to clear the flexgrid before you do any operations on it. To do that, use :-


Step 5

To add records to the Grid, use something like this.

CString strName,strRemarks;

m_nCount = 0;

// Clear and refresh the grid   

// 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


// 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

Step 6

To retrieve data from the Grid when the user double clicks on a record, add a double click message handler for the grid using the class wizard. Assuming that you have named the function as 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);    

Step 7

To move to the previous record, do this :-

void YourDialog::OnBtnPrevious()

    // Get the current selection

    int NextRow = m_Grid.GetRowSel();

    // If the position is at the last record, return
    if(NextRow <= 1)
        long BackColor[2],FontColor[2];

        int Column;

        // The BackColor and the FontColor variables 
        // are manipulated because we want a 
        // selected 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++)


        for(Column = 1; Column < m_Grid.GetCols(); Column++)


Step 8

To got to the Next record, do the reverse of the code above. Instead of

it would be m_Grid.SetRow(++NextRow).


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 too longer than usual. It would be more and more slow with each build. If you feel so, check the size of your .rc file. If its size is abnormal ( I cant specify a size because it depends on your project and its resource contents ),then 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 its a bug in flexgrid that corrupts or damages it. But from your side, be careful.


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionVC++, How we can add a record at the end of MSFlexGrid in VC++? Pin
Member 41874229-Oct-11 14:11
memberMember 41874229-Oct-11 14:11 
GeneralFlexgrid in VB6 Pin
Member 231839324-Sep-09 18:34
memberMember 231839324-Sep-09 18:34 
QuestionHow Should selected cell? Pin
Divyang Mithaiwala13-Mar-07 2:12
memberDivyang Mithaiwala13-Mar-07 2:12 
Questionresize the grid when the form is maximized Pin
Member #13214389-Mar-07 11:11
memberMember #13214389-Mar-07 11:11 
QuestionHow To Support Editing On Flex Grid Pin
MIAN KAMRAN22-Feb-07 22:12
memberMIAN KAMRAN22-Feb-07 22:12 
QuestionDoModal returns -1 when using FlexGrid [modified] Pin
al250028-Sep-06 16:10
memberal250028-Sep-06 16:10 
AnswerRe: DoModal returns -1 when using FlexGrid Pin
al250029-Sep-06 9:16
memberal250029-Sep-06 9:16 
GeneralShould not replace the original value in the combo box Pin
Sriram P.S2-May-06 21:13
memberSriram P.S2-May-06 21:13 
Generaladding combox in msflexgrid Pin
Sriram P.S28-Apr-06 1:03
memberSriram P.S28-Apr-06 1:03 
GeneralRe: adding combox in msflexgrid Pin
VGirish28-Apr-06 1:15
memberVGirish28-Apr-06 1:15 
GeneralRe: adding combox in msflexgrid Pin
Sriram P.S28-Apr-06 1:32
memberSriram P.S28-Apr-06 1:32 
QuestionWhat is the difference between FlexGrid and Heirarchical-FlexGrid ? Pin
cfilorux25-Apr-06 11:10
membercfilorux25-Apr-06 11:10 
Questionmsflexgrid redefinition error Pin
LucianoLiu25-Apr-06 4:07
memberLucianoLiu25-Apr-06 4:07 
AnswerRe: msflexgrid redefinition error Pin
cfilorux25-Apr-06 10:51
membercfilorux25-Apr-06 10:51 
GeneralRe: msflexgrid redefinition error Pin
LucianoLiu27-Apr-06 0:54
memberLucianoLiu27-Apr-06 0:54 
Generalmultiple selection Pin
fatih isikhan4-Jan-06 3:47
memberfatih isikhan4-Jan-06 3:47 
QuestionDesign-time error Pin
rankot7-Dec-05 20:55
memberrankot7-Dec-05 20:55 
AnswerRe: Design-time error Pin
cfilorux25-Apr-06 11:03
membercfilorux25-Apr-06 11:03 
GeneralkyLaiHow to add item to flexgrid Pin
topky20-Sep-05 17:35
membertopky20-Sep-05 17:35 
QuestionHow to add item to flexgrid Pin
topky20-Sep-05 17:34
membertopky20-Sep-05 17:34 
AnswerRe: How to add item to flexgrid Pin
rgordon9929-Sep-05 10:34
memberrgordon9929-Sep-05 10:34 
GeneralScroll event Pin
Anonymous24-May-05 4:48
sussAnonymous24-May-05 4:48 
GeneralSorting a column Pin
centuriansix19-May-05 20:22
membercenturiansix19-May-05 20:22 
Joke Pin 2:40
suss 2:40 
GeneralFlexgrid over Dialogbar Pin
anubis_ex12-May-05 3:10
memberanubis_ex12-May-05 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180314.2 | Last Updated 31 Jul 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid