CDataGrid Control


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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".

This article was originally published on August 17th, 2005


Most Popular Programming Stories

More for Developers

RSS Feeds

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