CDataGrid Control


This article is about a CDataGrid control programmed using Windows SDK. It is designed to be easy to use. The current version is not totally bug free, so it would be nice if you would report all detected bugs to have an update available soon.

The grid control is very similar to a MFC CListView control when it is in the "REPORT" view state. It supports a similar item, adding and removing and with custom item sorting with an application-defined comparison function.

The Code

How to use the CDataGrid control is explained here in detail. First of all, the header file "DataGrid.h" must be included in the project. Next, a variable of type CDataGrid must be declared.

#include "DataGrid.h"
CDataGrid dataGrid;

Next, call the Create method to create and show a DataGrid window, passing as parameters handle to parent window, window rectangle, and a number of columns DataGrid will have. Then, use the InsertItem method to add items to DataGrid, passing item text and alignment. A method SetItemInfo in two versions can be used to set subitem text, alignment, selection, read-only attribute, or to change background color. In the first version, the index of the item and subitem are passed, along with subitem text, alignment, or read-only attribute directly.

dataGrid.Create( wndRect, hParentWnd, 5 );
dataGrid.InsertItem( "Item1", DGTA_LEFT );
dataGrid.InsertItem( "Item2", DGTA_CENTER );
dataGrid.InsertItem( "Item3", DGTA_RIGHT );
dataGrid.SetItemInfo( 0, 1, "Subitem1", DGTA_CENTER, false );
dgii.dgMask     = DG_TEXTRONLY;
dgii.dgItem     = 1;
dgii.dgSubitem  = 0;
dgii.dgReadOnly = true;

To remove a single item, use RemoveItem, passing as the argument the index of the row that will be deleted.

Note: All indexing is zero-numbered. Also, calling the Update method is necessary after adding or removing items.

To remove all items, use RemoveAllItems, which has no agruments.


CDataGrid control has numerous features, as explained below.


These are the current features of DataGrid:

Automatic resizing with the parent window
Enabled when the Resize method is called each time the DataGrid parent window changes its size.
Enable/Disable sorting
Uses the EnableSort method.
Enable/Disable item text editing
Uses the EnableEdit method.
Enable/Disable column resizing
Uses the EnableResize method.
Enable/Disable grid
Uses the EnableGrid method.
Automatic scrolling to specified item
Uses the EnsureVisible method.
Automatic selection of specified item
Uses the SelectItem method.
Item sorting using custom application-defined comparison function
Uses the SetCompareFunction method.
Get/Set column text color
Uses the GetColumnTextColor and SetColumnTextColor methods.
Get/Set column font
Uses the GetColumnFont and SetColumnFont methods.
Get/Set row text color
Uses the GetRowTextColor and SetRowTextColor methods.
Get/Set row font
Uses the GetRowFont and SetRowFont methods.

I hope to extend this list of features as soon as possible.


CDataGridcontrol sends the following notification massages to its parent window via a WM_COMMAND message:

When focus is changed from one item to another.
When item/subitem text is changed.
When item is added.
When item is removed.
When column is resized.
When column is clicked.
When sorting is started.
When sorting is ended.

Also, this list of notifications will be extended.


The user can obtain all mentioned information and some more from the well-commented header file "DataGrid.h".



  • Grid not visible

    Posted by Tagarn on 03/16/2006 04:28am

    When there are not enough items to make vertical scroll bar displayed, all the grid is not shown.
    It seems that ShowScrollBar( m_hWnd, SB_BOTH, FALSE ); is responsible of that. 
    Is it a bug of MFC 8 (in VC2005) ?
    Changing code to ShowScrollBar( m_hWnd, SB_BOTH, TRUE );
    (line 78) corrects this problem.

  • A small fix.

    Posted by dinus on 10/04/2005 09:35am

    Thanks again for your code. I found a small problem and I want to propose a fix for it. When I try to create DataGrid with width which doesn't match the summary width of all columns and with content less than a page, DataGrid doesn't show. Here is how to easily recreate this situation: 1. Create a parent window, with coordinates 0, 0, 500, 500, rather than using CW_USEDEFAULT (DataGrid test.cpp, line 122). 2. Create a datagird of size 0, 0, 400, 400 (RECT rect={0, 0, 400, 400};, DataGrid test.cpp, line 348). 3. Add just 3 columns of size 100. 4. Add 10 rows. After starting DataGrid test.exe you will see that Data Grid doesn't show. I was able to fix this by adding the following line: SendMessage(dataGrid.GetWindowHandle(), WM_SIZE, 400, 400); right before datagrid.Update() (DataGrid test.cpp, line 385). Regards,

  • Very nice code.

    Posted by dinus on 08/20/2005 10:11am

    Very nice code for those who like low level programming. Thnks.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds