Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / MFC
Article

DataGrid Control

Rate me:
Please Sign up or sign in to vote.
3.31/5 (39 votes)
5 Aug 2005CPOL2 min read 188.2K   7.7K   61   54
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)


Written By
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: multiple row selection Pin
darkoman9-Jul-07 19:14
darkoman9-Jul-07 19:14 
QuestionHow to get the number of specific row Pin
kazim bhai17-May-07 6:46
kazim bhai17-May-07 6:46 
Questionmultiple instances Pin
hopsoid19-Nov-06 15:37
hopsoid19-Nov-06 15:37 
GeneralQuestion about simple applikation Pin
woj11727-Jul-06 8:40
woj11727-Jul-06 8:40 
NewsA bug Pin
kingren3-Jul-06 15:25
kingren3-Jul-06 15:25 
QuestionHidden Rows Pin
Rickomurphy27-Jun-06 4:13
Rickomurphy27-Jun-06 4:13 
AnswerRe: Hidden Rows Pin
darkoman27-Jun-06 5:14
darkoman27-Jun-06 5:14 
GeneralRe: Hidden Rows Pin
Rickomurphy27-Jun-06 5:53
Rickomurphy27-Jun-06 5:53 
Wow - that'd be great!
FYI the issues I've identified are:

(1) when the user clicks on a row to select it, converting the click position to the correct row given that there may be hidden rows. For example,
Row 1,2,3,8,9,10 are displayed (rows 4,5,6,7 are hidden)
Click on the 4th visible row, that means that row 8 has been selected.

(2) The vertical scrollbar must be resized depending on the number of visible rows

(3) Select Next/Prev must select the Next / Prev visible row

(4) When the currently selected row is hidden, it needs to be "un-selected"

(5) EnsureRowVisible needs to take account of hidden rows

Items (1) and (5) have got me stumped.
Item (2) required that a count is kept of the number of visible rows as well as the total number of rows. Keeping this in sync is a bit nasty when rows are added, removed, hidden and unhidden.

I look forward to the update! Thanks.

--Ricko
GeneralNice Pin
Sharpmike17-Jun-06 19:31
Sharpmike17-Jun-06 19:31 
GeneralRe: Nice Pin
darkoman17-Jun-06 20:25
darkoman17-Jun-06 20:25 
GeneralSome more wishes... Pin
woj11713-Mar-06 4:31
woj11713-Mar-06 4:31 
GeneralRe: Some more wishes... Pin
darkoman13-Mar-06 20:33
darkoman13-Mar-06 20:33 
GeneralRe: Some more wishes... Pin
woj11713-Mar-06 22:28
woj11713-Mar-06 22:28 
GeneralCDataGrid difficulties in SDI Pin
lukeevans8-Mar-06 23:49
lukeevans8-Mar-06 23:49 
GeneralRe: CDataGrid difficulties in SDI Pin
darkoman12-Mar-06 19:31
darkoman12-Mar-06 19:31 
GeneralRe: CDataGrid difficulties in SDI Pin
lukeevans13-Mar-06 19:24
lukeevans13-Mar-06 19:24 
GeneralSome improves Pin
WiseWarrior4-Jan-06 23:06
WiseWarrior4-Jan-06 23:06 
GeneralRe: Some improves Pin
darkoman5-Jan-06 1:03
darkoman5-Jan-06 1:03 
GeneralRe: Some improves Pin
WiseWarrior6-Jan-06 20:45
WiseWarrior6-Jan-06 20:45 
GeneralRe: Some improves Pin
darkoman6-Jan-06 21:39
darkoman6-Jan-06 21:39 
GeneralAnother Bug too! Pin
Love In Snowing11-Jul-05 2:02
Love In Snowing11-Jul-05 2:02 
GeneralRe: Another Bug too! Pin
darkoman21-Jul-05 1:20
darkoman21-Jul-05 1:20 
GeneralThis code does not work! Pin
PJ Arends10-Jul-05 11:45
professionalPJ Arends10-Jul-05 11:45 
GeneralRe: This code does not work! Pin
darkoman21-Jul-05 1:21
darkoman21-Jul-05 1:21 
GeneralAnother bug Pin
David O'Neil9-Jul-05 8:49
professionalDavid O'Neil9-Jul-05 8:49 

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.