Click here to Skip to main content
11,480,502 members (41,533 online)
Click here to Skip to main content

DataGrid Control

, 5 Aug 2005 CPOL 119.4K 5.4K 56
Rate this:
Please Sign up or sign in to vote.
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

GeneralRe: CDataGrid difficulties in SDI Pin
darkoman12-Mar-06 20:31
memberdarkoman12-Mar-06 20:31 
GeneralRe: CDataGrid difficulties in SDI Pin
lukeevans13-Mar-06 20:24
memberlukeevans13-Mar-06 20:24 
GeneralSome improves Pin
WiseWarrior5-Jan-06 0:06
memberWiseWarrior5-Jan-06 0:06 
GeneralRe: Some improves Pin
darkoman5-Jan-06 2:03
memberdarkoman5-Jan-06 2:03 
GeneralRe: Some improves Pin
WiseWarrior6-Jan-06 21:45
memberWiseWarrior6-Jan-06 21:45 
GeneralRe: Some improves Pin
darkoman6-Jan-06 22:39
memberdarkoman6-Jan-06 22:39 
GeneralAnother Bug too! Pin
Love In Snowing11-Jul-05 3:02
memberLove In Snowing11-Jul-05 3:02 
GeneralRe: Another Bug too! Pin
darkoman21-Jul-05 2:20
memberdarkoman21-Jul-05 2:20 
GeneralThis code does not work! Pin
PJ Arends10-Jul-05 12:45
memberPJ Arends10-Jul-05 12:45 
GeneralRe: This code does not work! Pin
darkoman21-Jul-05 2:21
memberdarkoman21-Jul-05 2:21 
GeneralAnother bug Pin
David O'Neil9-Jul-05 9:49
memberDavid O'Neil9-Jul-05 9:49 
GeneralRe: Another bug Pin
darkoman21-Jul-05 2:23
memberdarkoman21-Jul-05 2:23 
Generalgood job Pin
badpeos8-Jul-05 16:06
memberbadpeos8-Jul-05 16:06 
GeneralNice, but... Pin
Kochise8-Jul-05 4:49
memberKochise8-Jul-05 4:49 
7 warnings, from 'unreferenced local variables' to... err... 'not all control paths return a value' !

Select between Column0 and Column1, then drag the limit completely on the left, before Column0 (width == 0).

Select between Column1 and Column2, then drag the limit completely on the left, before Column0 (width == -width Column0).

Sort the lines with clicking the following clomuns header in the following order :

Column0 6 times

The message grid don't automatically scroll down when too much messages comes.

Would like to see a tree on Column0 if I want...

Beside all of this, you get my 4. Right now I suspend my vote, I'd like to give you a 5 Smile | :) Thanks for updating things... And debug more carefully, please :/


In Code we trust !
GeneralRe: Nice, but... Pin
darkoman21-Jul-05 2:24
memberdarkoman21-Jul-05 2:24 

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
Web03 | 2.8.150520.1 | Last Updated 5 Aug 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid