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

  • Contact center infrastructure vendors continue to integrate their multichannel capabilities to develop ''omnichannel'' solutions, while enhancing their cloud delivery capabilities. Gartner Magic Quadrant reports evaluate contact center solution providers based on completeness of vision and ability to execute. As you are looking to modernize your existing on premises contact center solution or considering cloud as an alternative, check out these Gartner Reports for strengths and cautions to consider when …

  • Like many roles in the C-Suite, the CIO role is also changing. Its impact and control have been circumvented by cloud computing, the consumerization of IT, and employees that now have unprecedented access to technology. The role of the CIO is at a critical juncture. For some CIOs, the changing landscape reduces their scope of influence. For others, it means unprecedented opportunity. This eBook explores the changes that CIOs are experiencing today and how these executives can leverage their expertise to help …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date