Click here to Skip to main content
13,295,178 members (69,650 online)
Click here to Skip to main content
Add your own
alternative version


59 bookmarked
Posted 8 Jul 2005

DataGrid Control

, 5 Aug 2005
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.

You may also be interested in...


Comments and Discussions

Questionhow to scrol datagrid Pin
Gener47210-Dec-14 4:06
memberGener47210-Dec-14 4:06 
QuestionErrors remove and update version, can be downloaded here Pin
maplewang4-Nov-14 6:52
membermaplewang4-Nov-14 6:52 
Questiondatagrid Pin
nibble_bits16-Jul-13 8:07
membernibble_bits16-Jul-13 8:07 
GeneralMore memory leaks.... Pin
dc_20001-Apr-11 13:39
memberdc_20001-Apr-11 13:39 
GeneralWow, this thing leaks memory like a seive Pin
dc_200024-Mar-11 10:36
memberdc_200024-Mar-11 10:36 
Questionmemory leaks Pin
RonWilsonCPP11-Aug-10 22:15
memberRonWilsonCPP11-Aug-10 22:15 
AnswerRe: memory leaks [modified] Pin
maplewang7-Dec-10 19:24
membermaplewang7-Dec-10 19:24 
GeneralLogical Flaw Pin
JosephHong31-Jul-09 23:00
memberJosephHong31-Jul-09 23:00 
GeneralRe: Logical Flaw Pin
dc_200025-Mar-11 15:48
memberdc_200025-Mar-11 15:48 
AnswerControl Not Displaying Bug and Fix Pin
Drew_Benton8-Apr-08 0:54
memberDrew_Benton8-Apr-08 0:54 
QuestionLicense? Pin
recyclotron20-Feb-08 6:22
memberrecyclotron20-Feb-08 6:22 
QuestionNice and useful Pin
spyto30-Nov-07 4:51
memberspyto30-Nov-07 4:51 
QuestionHow to resize the Datagrid? Pin
lurvhua26-Nov-07 19:17
memberlurvhua26-Nov-07 19:17 
AnswerRe: How to resize the Datagrid? Pin
Ajm11330-Dec-09 8:48
memberAjm11330-Dec-09 8:48 
Generala bug Pin
clxye8-Oct-07 18:24
memberclxye8-Oct-07 18:24 
QuestionHow to delete a column? Pin
clxye27-Sep-07 23:16
memberclxye27-Sep-07 23:16 
AnswerRe: How to delete a column? Pin
oldmoon27-Sep-07 23:37
memberoldmoon27-Sep-07 23:37 
GeneralRe: How to delete a column? Pin
clxye28-Sep-07 16:01
memberclxye28-Sep-07 16:01 
GeneralRe: How to delete a column? Pin
oldmoon29-Sep-07 16:35
memberoldmoon29-Sep-07 16:35 
AnswerRe: How to delete a column? Pin
clxye28-Sep-07 21:47
memberclxye28-Sep-07 21:47 
GeneralProblem In Unicode Pin
lurvhua30-Aug-07 23:08
memberlurvhua30-Aug-07 23:08 
GeneralRe: Problem In Unicode Pin
darkoman31-Aug-07 13:12
memberdarkoman31-Aug-07 13:12 
GeneralRe: Problem In Unicode Pin
lurvhua2-Sep-07 1:18
memberlurvhua2-Sep-07 1:18 
Generalmultiple row selection Pin
adii289-Jul-07 7:43
memberadii289-Jul-07 7:43 
GeneralRe: multiple row selection Pin
darkoman9-Jul-07 20:14
memberdarkoman9-Jul-07 20:14 
QuestionHow to get the number of specific row Pin
eryreyeryeryeryeyryye17-May-07 7:46
membereryreyeryeryeryeyryye17-May-07 7:46 
Questionmultiple instances Pin
hopsoid19-Nov-06 16:37
memberhopsoid19-Nov-06 16:37 
GeneralQuestion about simple applikation Pin
woj11727-Jul-06 9:40
memberwoj11727-Jul-06 9:40 
NewsA bug Pin
kingren3-Jul-06 16:25
memberkingren3-Jul-06 16:25 
QuestionHidden Rows Pin
Rickomurphy27-Jun-06 5:13
memberRickomurphy27-Jun-06 5:13 
AnswerRe: Hidden Rows Pin
darkoman27-Jun-06 6:14
memberdarkoman27-Jun-06 6:14 
GeneralRe: Hidden Rows Pin
Rickomurphy27-Jun-06 6:53
memberRickomurphy27-Jun-06 6:53 
GeneralNice Pin
Sharpmike17-Jun-06 20:31
memberSharpmike17-Jun-06 20:31 
Nice work, you got my 4. Not many are willing to do this kind of thing in Win32; even fewer are willing to share their code for free. Love the grid, but I have a few suggestions for you. Ignore these if you've heard them before.

- Try drawing only those cells that are visible. You can greatly speed up the responsiveness of the grid when there are 500+ cells.
- Try to implement more functions as windows messages. This would make wrapping your control in an MFC, WTL, or other object windows environment much easier.
- Provide a UNICODE implementation. I see an earlier post suggesting this; you're right, it is simple to do.
- Consider XP theming. It's very easy to implement and adds a lot of visual bang for the buck.
- Build yourself a way of subclassing the edit box and any other controls you wish to use for editing the grid. This way you can catch things like the Enter, Escape, and Tab keys and perform appropriate actions in the grid.
- Implement a handler for WM_GETDLGCODE. This way users putting your grid in a dialog can still get the standard behavior for any keyboard input on the grid.

Thanks again for your hard work, I appreciate it.

GeneralRe: Nice Pin
darkoman17-Jun-06 21:25
memberdarkoman17-Jun-06 21:25 
GeneralSome more wishes... Pin
woj11713-Mar-06 5:31
memberwoj11713-Mar-06 5:31 
GeneralRe: Some more wishes... Pin
darkoman13-Mar-06 21:33
memberdarkoman13-Mar-06 21:33 
GeneralRe: Some more wishes... Pin
woj11713-Mar-06 23:28
memberwoj11713-Mar-06 23:28 
GeneralCDataGrid difficulties in SDI Pin
lukeevans9-Mar-06 0:49
memberlukeevans9-Mar-06 0:49 
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 

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