Home arrow C++ arrow Page 4 - Programming MSFlexGrid in MFC (VC++)

Programming MSFlexGrid in MFC (VC++)

Representation of data is one the central aspects of any application. Tabular representation of data is the most common method of representing given data. Each language/framework provides its own technique, in the form of controls, to present data in tabular format. MFC (Microsoft Foundation Classes) is no exception. One of the controls that it provides is the MSFlexGrid control.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
January 28, 2009
  1. · Programming MSFlexGrid in MFC (VC++)
  2. · Using MSFlexGrid: Step by Step
  3. · MSFlexGrid in the Real World
  4. · More Code

print this article

Programming MSFlexGrid in MFC (VC++) - More Code
(Page 4 of 4 )

//This function is called to update the grid whenever a user leaves a cell

//after making some changes to any cell of the grid.

void CEditGrid::OnUpdateGrid()


//Check to see if edit is visible

BOOL bVisible = ::GetWindowLong(m_edit.GetSafeHwnd(), GWL_STYLE) & WS_VISIBLE;

if (bVisible)


CString cStr;





GetTextGrid(); //This function is called from here to get the //multiple digit inputs from the grid



Next we need to set up the part of the program that saves the entered data. In the handler for the OK button of the dialog box, provide the following code:



CString inpx[50];

CString inpy[50];

FILE *fp;

// TODO: Add extra validation here

int k = m_grid.count;

int f = m_grid.i;

int g = m_grid.j;

int a , b;

a = 0;

b = 0;

fp = fopen("dat.txt", "w");

for(int q = 0; q < f; q++)



inpx[a] = m_grid.inputx[q][0];

fprintf(fp, " a=%d %dn",a,atoi(inpx[a]) );



for(int m = 0;m < g; m++)



inpy[b] = m_grid.inputy[m][1];

fprintf(fp, " b=%d %dn",b,atoi(inpy[b]));







The first step is to declare the arrays to hold values of the two columns. Here the arrays are named inpx and inpy. Then the total count of the cells, the count of two columns, is retrieved.

The next step is to open the file to write out the input values. To do so, the example makes use of conventional File I/O API. Then the arrays (inpx and inpy) are populated with the values of the two columns. The arrays are populated by iterating over the values of the instance level arrays containing the values entered by the user. Finally, each value is converted to an integer and saved in the file.

That completes the small application that makes use of FlexGrid. However, this leaves a question regarding the serialization of the values in MFC. The answer is there is an API to do so. That will be discussed in the future. Till then...

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials