Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Implementation

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

m_Grid.Clear();

Step 5

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

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();

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()
{
    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 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++)
        {
            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);
    }
}

Step 8

To got 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 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.

Conclusion

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.

License

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

Share

About the Author

VGirish
Founder
India India
No Biography provided

Comments and Discussions

 
QuestionVC++, How we can add a record at the end of MSFlexGrid in VC++? PinmemberMember 41874229-Oct-11 15:11 
GeneralFlexgrid in VB6 PinmemberMember 231839324-Sep-09 19:34 
QuestionHow Should selected cell? PinmemberDivyang Mithaiwala13-Mar-07 3:12 
Questionresize the grid when the form is maximized PinmemberMember #13214389-Mar-07 12:11 
QuestionHow To Support Editing On Flex Grid PinmemberMIAN KAMRAN22-Feb-07 23:12 
QuestionDoModal returns -1 when using FlexGrid [modified] Pinmemberal250028-Sep-06 17:10 
AnswerRe: DoModal returns -1 when using FlexGrid Pinmemberal250029-Sep-06 10:16 
GeneralShould not replace the original value in the combo box PinmemberSriram P.S2-May-06 22:13 
Generaladding combox in msflexgrid PinmemberSriram P.S28-Apr-06 2:03 
GeneralRe: adding combox in msflexgrid PinmemberVGirish28-Apr-06 2:15 
GeneralRe: adding combox in msflexgrid PinmemberSriram P.S28-Apr-06 2:32 
QuestionWhat is the difference between FlexGrid and Heirarchical-FlexGrid ? Pinmembercfilorux25-Apr-06 12:10 
Questionmsflexgrid redefinition error PinmemberLucianoLiu25-Apr-06 5:07 
AnswerRe: msflexgrid redefinition error Pinmembercfilorux25-Apr-06 11:51 
GeneralRe: msflexgrid redefinition error PinmemberLucianoLiu27-Apr-06 1:54 
Generalmultiple selection Pinmemberfatih isikhan4-Jan-06 4:47 
QuestionDesign-time error Pinmemberrankot7-Dec-05 21:55 
AnswerRe: Design-time error Pinmembercfilorux25-Apr-06 12:03 
GeneralkyLaiHow to add item to flexgrid Pinmembertopky20-Sep-05 18:35 
QuestionHow to add item to flexgrid Pinmembertopky20-Sep-05 18:34 
AnswerRe: How to add item to flexgrid Pinmemberrgordon9929-Sep-05 11:34 
GeneralScroll event PinsussAnonymous24-May-05 5:48 
GeneralSorting a column Pinmembercenturiansix19-May-05 21:22 
Jokehttp://deramaxx.ceroline.info/ Pinsusshttp://deramaxx.ceroline.info/5-Dec-07 3:40 
GeneralFlexgrid over Dialogbar Pinmemberanubis_ex12-May-05 4:10 
GeneralRe: Flexgrid over Dialogbar Pinmemberanubis_ex15-Jul-06 11:01 
GeneralSetScrollPos( ) not moving grid PinsussAnonymous4-May-05 2:40 
GeneralRe: SetScrollPos( ) not moving grid PinmemberDuman Nir10-Aug-06 1:34 
QuestionHow do you add Row Headers PinsussAnonymous6-Apr-05 8:45 
AnswerRe: How do you add Row Headers PinsussAnonymous6-Apr-05 9:08 
QuestionHow to change Font Pinmemberprasad.morkar26-Mar-05 20:36 
GeneralFlexGrid in Tab Control Dialog PinmemberMilan Risto16-Feb-05 22:57 
QuestionHow to select multiple cells using ctrl key? Pinmemberparashar_ranjana@yahoo.com12-Oct-04 21:46 
GeneralFlexGrid Rows Navigation in Dynamic CWnd View PinsussAnonymousVCGuy6-May-04 22:39 
QuestionHow can I put background-color into some special cells? Pinmemberliushan15-Mar-04 23:33 
QuestionHow can insert CheckBox in FlexGrid? Pinmemberholysmoke23-Feb-04 23:15 
AnswerRe: How can insert CheckBox in FlexGrid? Pinmemberbeaulah15-Jul-04 7:22 
GeneralRe: How can insert CheckBox in FlexGrid? Pinmembersarathbs25-Feb-07 22:58 
GeneralRe: How can insert CheckBox in FlexGrid? Pinmembershah sachin n22-Sep-08 5:06 
QuestionHow can I let user type in my grid? PinsussKoorosh Pedram29-Jan-04 8:12 
AnswerRe: How can I let user type in my grid? Pinmemberbeaulah5-Oct-05 21:13 
GeneralOwner draw cell PinmemberAppstmd23-Dec-03 2:21 
QuestionHow to use Flexgrid in a DLL PinmemberLuisM24-Nov-03 2:22 
AnswerRe: How to use Flexgrid in a DLL PinmemberAnon E. Mouse29-Jan-04 9:27 
Generalmissing flexgrid methods PinsussAnonymous21-Oct-03 18:36 
GeneralRe: missing flexgrid methods PinsussAnonymous21-Oct-03 18:55 
GeneralRe: missing flexgrid methods Pinmemberfrodi12-Aug-05 8:07 
QuestionHow to use SetDataSource?? PinmemberAmyLoo15-Jul-03 17:23 
GeneralProblem on MSFlexGrid pointer type PinmemberArash Hemmati25-Jun-03 23:17 
GeneralRe: Problem on MSFlexGrid pointer type PinmemberMAK4maX10-Feb-04 19:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 31 Jul 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid