Click here to Skip to main content
14,828,290 members
Articles / Desktop Programming / MFC
Posted 8 Jul 2005


59 bookmarked

DataGrid Control

Rate me:
Please Sign up or sign in to vote.
3.21/5 (37 votes)
5 Aug 2005CPOL2 min read
An article on a grid control built without MFC.

Example on using DataGrid control


This article presents a DataGrid control which is built with no MFC. It can be used in SDK or MFC Win32 applications. This source code is also compiled with GNU compiler and has shown to be stable.


You can find various grid controls all over the Internet, some free and some not. Also, there is an article by Chris Maunder about a grid control which can be used on different platforms (ATL and MFC version). Grid controls are very useful for representing two-dimensional tabular data. They are often used in accounting applications. Grid controls must be designed for quick data-referencing and modification. The grid control presented in this article supports up to 32000 rows and 1000 columns. Also, there can be up to 20 grid controls created at the same time. These values can be changed in the DataGrid header file but there is always a memory limit.

Using the code

To use the DataGrid control a header file must be included in the project.

#include "DataGrid.h"

Next, create an instance of the CDataGrid class and call the Create() method.

// hParentWnd is declared somewhere else
CDataGrid dataGrid;
int numCols = 5;
RECT rect = {0,0,500,300};
dataGrid.Create( rect, hParentWnd, numCols );

Use SetColumnInfo() method to describe the DataGrid control columns.

int colIndex = 0;
char colText[] = "Column1";
int colSize = 120;
UINT txtAlign = DGTA_LEFT;
dataGrid.SetColumnInfo( colIndex, colText, colSize, txtAlign );

To add items to the DataGrid control, call InsertItem() method.

char itemText[] = "Item1";
dataGrid.InsertItem( itemText, txtAlign );

To describe subitems, use SetItemInfo() method.

int rowIndex = 0;
int columnIndex = 0;
char subitemText[] = "Subitem1";
bool readOnly = false;
dataGrid.SetItemInfo( rowIndex, columnIndex, subitemText, txtAlign, readOnly );

The DataGrid control sends notification messages through the WM_COMMAND message. These notifications are:

  • Item changed
  • Item text changed
  • Item added
  • Item removed
  • Column resized
  • Column clicked
  • Sorting started
  • Sorting ended

This is the basic use of this control. See the demo project as an example. The DataGrid control supports the following:

  • Grid show (on/off)
  • Column resize (on/off)
  • Item text edit (on/off)
  • Items sorting (on/off)
  • Get/set row background color
  • Get/set row text color
  • Get/set row font
  • Get/set column text color
  • Get/set column font
  • Set application-defined sort function

Points of Interest

My goal was to try to develop a grid control that will support most of the things that the MFC CListCtrl control does and possibly some more and to be as efficient. Its GUI is designed to be very similar with the previously mentioned control.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions

QuestionHow to disable edit control when click out datagrid? Pin
Gem Lee12-Jun-20 1:52
MemberGem Lee12-Jun-20 1:52 
Questionhow to scrol datagrid Pin
Gener47210-Dec-14 3:06
MemberGener47210-Dec-14 3:06 
QuestionErrors remove and update version, can be downloaded here Pin
maplewang4-Nov-14 5:52
Membermaplewang4-Nov-14 5:52 
Questiondatagrid Pin
nibble_bits16-Jul-13 7:07
Membernibble_bits16-Jul-13 7:07 
GeneralMore memory leaks.... Pin
dc_20001-Apr-11 12:39
Memberdc_20001-Apr-11 12:39 
GeneralWow, this thing leaks memory like a seive Pin
dc_200024-Mar-11 9:36
Memberdc_200024-Mar-11 9:36 
Questionmemory leaks Pin
RonWilsonCPP11-Aug-10 21:15
MemberRonWilsonCPP11-Aug-10 21:15 
AnswerRe: memory leaks [modified] Pin
maplewang7-Dec-10 18:24
Membermaplewang7-Dec-10 18:24 
GeneralLogical Flaw Pin
JosephHong31-Jul-09 22:00
MemberJosephHong31-Jul-09 22:00 
GeneralRe: Logical Flaw Pin
dc_200025-Mar-11 14:48
Memberdc_200025-Mar-11 14:48 
AnswerControl Not Displaying Bug and Fix Pin
Drew_Benton7-Apr-08 23:54
MemberDrew_Benton7-Apr-08 23:54 
QuestionLicense? Pin
recyclotron20-Feb-08 5:22
Memberrecyclotron20-Feb-08 5:22 
QuestionNice and useful Pin
spyto30-Nov-07 3:51
Memberspyto30-Nov-07 3:51 
QuestionHow to resize the Datagrid? Pin
lurvhua26-Nov-07 18:17
Memberlurvhua26-Nov-07 18:17 
AnswerRe: How to resize the Datagrid? Pin
Ajm11330-Dec-09 7:48
MemberAjm11330-Dec-09 7:48 
Generala bug Pin
clxye8-Oct-07 17:24
Memberclxye8-Oct-07 17:24 
QuestionHow to delete a column? Pin
clxye27-Sep-07 22:16
Memberclxye27-Sep-07 22:16 
AnswerRe: How to delete a column? Pin
oldmoon27-Sep-07 22:37
Memberoldmoon27-Sep-07 22:37 
GeneralRe: How to delete a column? Pin
clxye28-Sep-07 15:01
Memberclxye28-Sep-07 15:01 
GeneralRe: How to delete a column? Pin
oldmoon29-Sep-07 15:35
Memberoldmoon29-Sep-07 15:35 
AnswerRe: How to delete a column? Pin
clxye28-Sep-07 20:47
Memberclxye28-Sep-07 20:47 
GeneralProblem In Unicode Pin
lurvhua30-Aug-07 22:08
Memberlurvhua30-Aug-07 22:08 
GeneralRe: Problem In Unicode Pin
darkoman31-Aug-07 12:12
Memberdarkoman31-Aug-07 12:12 
GeneralRe: Problem In Unicode Pin
lurvhua2-Sep-07 0:18
Memberlurvhua2-Sep-07 0:18 
Generalmultiple row selection Pin
adii289-Jul-07 6:43
Memberadii289-Jul-07 6:43 

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.