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

DataGrid Control

By , 5 Aug 2005
 

Example on using DataGrid control

Introduction

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.

Background

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.

License

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

About the Author

darkoman
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMore memory leaks....memberdc_20001 Apr '11 - 12:39 
GeneralWow, this thing leaks memory like a seivememberdc_200024 Mar '11 - 9:36 
Questionmemory leaksmemberRonWilsonCPP11 Aug '10 - 21:15 
AnswerRe: memory leaks [modified]membermaplewang7 Dec '10 - 18:24 
GeneralLogical FlawmemberJosephHong31 Jul '09 - 22:00 
GeneralRe: Logical Flawmemberdc_200025 Mar '11 - 14:48 
AnswerControl Not Displaying Bug and FixmemberDrew_Benton7 Apr '08 - 23:54 
QuestionLicense?memberrecyclotron20 Feb '08 - 5:22 
QuestionNice and usefulmemberspyto30 Nov '07 - 3:51 
QuestionHow to resize the Datagrid?memberlurvhua26 Nov '07 - 18:17 
AnswerRe: How to resize the Datagrid?memberAjm11330 Dec '09 - 7:48 
Generala bugmemberclxye8 Oct '07 - 17:24 
QuestionHow to delete a column?memberclxye27 Sep '07 - 22:16 
AnswerRe: How to delete a column?memberoldmoon27 Sep '07 - 22:37 
GeneralRe: How to delete a column?memberclxye28 Sep '07 - 15:01 
GeneralRe: How to delete a column?memberoldmoon29 Sep '07 - 15:35 
AnswerRe: How to delete a column?memberclxye28 Sep '07 - 20:47 
GeneralProblem In Unicodememberlurvhua30 Aug '07 - 22:08 
GeneralRe: Problem In Unicodememberdarkoman31 Aug '07 - 12:12 
GeneralRe: Problem In Unicodememberlurvhua2 Sep '07 - 0:18 
Generalmultiple row selectionmemberadii289 Jul '07 - 6:43 
GeneralRe: multiple row selectionmemberdarkoman9 Jul '07 - 19:14 
QuestionHow to get the number of specific rowmembereryreyeryeryeryeyryye17 May '07 - 6:46 
Questionmultiple instancesmemberhopsoid19 Nov '06 - 15:37 
GeneralQuestion about simple applikationmemberwoj11727 Jul '06 - 8:40 
NewsA bugmemberkingren3 Jul '06 - 15:25 
QuestionHidden RowsmemberRickomurphy27 Jun '06 - 4:13 
AnswerRe: Hidden Rowsmemberdarkoman27 Jun '06 - 5:14 
GeneralRe: Hidden RowsmemberRickomurphy27 Jun '06 - 5:53 
GeneralNicememberSharpmike17 Jun '06 - 19:31 
GeneralRe: Nicememberdarkoman17 Jun '06 - 20:25 
GeneralSome more wishes...memberwoj11713 Mar '06 - 4:31 
GeneralRe: Some more wishes...memberdarkoman13 Mar '06 - 20:33 
GeneralRe: Some more wishes...memberwoj11713 Mar '06 - 22:28 
GeneralCDataGrid difficulties in SDImemberlukeevans8 Mar '06 - 23:49 
GeneralRe: CDataGrid difficulties in SDImemberdarkoman12 Mar '06 - 19:31 
GeneralRe: CDataGrid difficulties in SDImemberlukeevans13 Mar '06 - 19:24 
GeneralSome improvesmemberWiseWarrior4 Jan '06 - 23:06 
GeneralRe: Some improvesmemberdarkoman5 Jan '06 - 1:03 
GeneralRe: Some improvesmemberWiseWarrior6 Jan '06 - 20:45 
GeneralRe: Some improvesmemberdarkoman6 Jan '06 - 21:39 
GeneralAnother Bug too!memberLove In Snowing11 Jul '05 - 2:02 
GeneralRe: Another Bug too!memberdarkoman21 Jul '05 - 1:20 
GeneralThis code does not work!memberPJ Arends10 Jul '05 - 11:45 
GeneralRe: This code does not work!memberdarkoman21 Jul '05 - 1:21 
GeneralAnother bugmemberDavid O'Neil9 Jul '05 - 8:49 
GeneralRe: Another bugmemberdarkoman21 Jul '05 - 1:23 
Generalgood jobmemberbadpeos8 Jul '05 - 15:06 
GeneralNice, but...memberKochise8 Jul '05 - 3:49 
GeneralRe: Nice, but...memberdarkoman21 Jul '05 - 1:24 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 5 Aug 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid