Click here to Skip to main content
Licence CPOL
First Posted 8 Jul 2005
Views 92,384
Bookmarked 49 times

DataGrid Control

By | 5 Aug 2005 | Article
An article on a grid control built without MFC.

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMore memory leaks.... Pinmemberdc_200012:39 1 Apr '11  
GeneralWow, this thing leaks memory like a seive Pinmemberdc_20009:36 24 Mar '11  
Questionmemory leaks PinmemberRonWilsonCPP21:15 11 Aug '10  
AnswerRe: memory leaks [modified] Pinmembermaplewang18:24 7 Dec '10  
GeneralLogical Flaw PinmemberJosephHong22:00 31 Jul '09  
GeneralRe: Logical Flaw Pinmemberdc_200014:48 25 Mar '11  
AnswerControl Not Displaying Bug and Fix PinmemberDrew_Benton23:54 7 Apr '08  
QuestionLicense? Pinmemberrecyclotron5:22 20 Feb '08  
QuestionNice and useful Pinmemberspyto3:51 30 Nov '07  
QuestionHow to resize the Datagrid? Pinmemberlurvhua18:17 26 Nov '07  
AnswerRe: How to resize the Datagrid? PinmemberAjm1137:48 30 Dec '09  
Generala bug Pinmemberclxye17:24 8 Oct '07  
QuestionHow to delete a column? Pinmemberclxye22:16 27 Sep '07  
AnswerRe: How to delete a column? Pinmemberoldmoon22:37 27 Sep '07  
GeneralRe: How to delete a column? Pinmemberclxye15:01 28 Sep '07  
GeneralRe: How to delete a column? Pinmemberoldmoon15:35 29 Sep '07  
AnswerRe: How to delete a column? Pinmemberclxye20:47 28 Sep '07  
GeneralProblem In Unicode Pinmemberlurvhua22:08 30 Aug '07  
GeneralRe: Problem In Unicode Pinmemberdarkoman12:12 31 Aug '07  
GeneralRe: Problem In Unicode Pinmemberlurvhua0:18 2 Sep '07  
Generalmultiple row selection Pinmemberadii286:43 9 Jul '07  
GeneralRe: multiple row selection Pinmemberdarkoman19:14 9 Jul '07  
QuestionHow to get the number of specific row Pinmembereryreyeryeryeryeyryye6:46 17 May '07  
Questionmultiple instances Pinmemberhopsoid15:37 19 Nov '06  
GeneralQuestion about simple applikation Pinmemberwoj1178:40 27 Jul '06  

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.

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