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