Introduction
This is a data grid built using ATL 3.0, STL and Win32 API. The article provides some explanation about the grid's capabilities. I will not describe in detail the implementation of my grid because there is a lot of code and I do not think that anybody would have enough patience to read something like that. Source code is commented and this should be enough.
Data types
As one can see, every column of the grid has an associated data type. This means that every value in the same column will have the same type. Available types are:
typedef [v1_enum] enum DataType
{
DataType_Empty = 0,
DataType_Null = 1,
DataType_I2 = 2,
DataType_I4 = 3,
DataType_R4 = 4,
DataType_R8 = 5,
DataType_Cy = 6,
DataType_Date = 7,
DataType_BStr = 8,
DataType_Dispatch = 9,
DataType_Error = 10,
DataType_Bool = 11,
DataType_Variant = 12,
DataType_Unknown = 13,
DataType_UI1 = 17,
DataType_Option = 0x0100,
DataType_StrictOption
} DataType;
As everybody can see, these types are in fact almost all VARTYPEs. If the user tries to change the value of a cell with a value whose type is incompatible with the type associated with the column, the value of the cell will not change. I also added some custom types, like Option
and StrictOption
. Data of these types are edited using combo boxes and drop down lists, as you can see in the next picture.
Range specifications
Every column has a type and a range for values. This means that you can enter in the cells, values that have the type associated with the column and are in the specified range. For example, you can specify ranges for numeric values:"18,49" or you can specify options that are available for an Option
or StrictOption
type: "Male|Female". For numeric values, the possibilities to specify ranges are:
",nMax"
This means that values in the column are restricted to be less or equal than nMax
value
"nMin,"
Values in the column are restricted to be greater or equal than nMax
value
"nMin,nMax"
Values in the column cannot be changed by the user in order to be outside of the interval [nMin,nMax]
User operations
Row selection is also available. You can select multiple rows of the grid using right-click. Clicking the upper-left corner of the grid selects all rows in the grid. Double clicking the same area unselects all lines. From VB, for example, you can test if a row is selected or not. Optionally, rows can be numbered on the left header. Pressing enter or F2 enters the grid in edit mode. Depending on the type of the data that is in the selected cell, on edit mode opens an edit control, combo box, drop down list or the value is automatically changed ( boolean values ). Sorting is available. If a column's property AllowUserToSort
is set on TRUE
, then double-clicking that column sorts the rows after the values on that column. If the user modifies that column and the content becomes unsorted, pressing F5 makes a resort.
Static and dynamic resizing is available for top header height, left header width, row height and column width. In the previous image, I have marked with red dots the points that can be used for resizing.
Colors are fully customizable. You can choose your favorite combination of colors for grid items, as you can see in the screenshot.
Data sources
You can fill the grid with data from code (VB is the easiest way to manipulate this grid). Also, there are two more ways to enter data into the grid: a previously opened ADO Recordset, a connection string or an SQL SELECT
statement. Unfortunately, in this version of the grid, the databases are not automatically updated when the values of the cells change. This will be done in the next version.
typedef [v1_enum] enum
{
DataSourceType_None = 0,
DataSourceType_ADORecordset,
DataSourceType_SQLStatement,
} DataSourceType;
Database connection is not very well implemented yet, so I recommend this grid to be used with DataSourceType_None
.
Grid manipulation
I will describe now the interfaces that can be used to manipulate grid elements.
ISmartGrid
interace ISmartGrid : IDispatch;
ISmartGrid
interface is used to manipulate general properties of the grid. ISmartGrid
is a big interface so I will describe its methods and properties without inserting the IDL code here.
Methods
AddColumn( [in] Name, [in] Type, [in, optional] ReadOnly,
[in, optional] Range, [out, optional] Column )
InsertColumn( [in] Name, [in] Type, [in] Position, [in, optional] ReadOnly,
[in, optional] Range, [out, optional] Column )
RemoveColumn( [in] Position )
AddRow( [out, optional] Row )
InsertRow( [in] Position, [out, optional] Row )
RemoveRow( [in] Position )
SelectAllRows()
UnselectAllRows()
Requery()
Properties
ActiveColumn
ActiveRow
NumberOfColumns
NumberOfRows
ReadOnly
UILocked
Column
ConnectionString
CursorType
DataSource
DataSourceType
LockType
Row
ActiveRowColor
ContentBkColor
ContentForeColor
ContentLinesColor
DefaultColumnWidth
Font
HeaderBkCOlor
HeaderForeColor
HeaderLinesColor
LeftHeaderVisible
LeftHeaderWidth
RowHeight
SelectedItemColor
TopHeaderHeight
TopHeaderVisible
Using methods from ISmartGrid
interface, columns and rows can be added to the grid. When adding a column or a row, a reference to it is optionally returned, in order to be used immediately after it is inserted. I split the properties of the grid into three sections. One is called State and I grouped here all properties that refers to the state of the grid. The second group refers to Data Access and the third group of properties can be used to customize User Interface.
ICell
[
helpstring("ICell interface is used to access cells"),
uuid(C6462BB2-E639-11d3-8138-0000B49DBD2E),
pointer_default(unique)
]
interface ICell : IUnknown
{
[propget, helpstring("property Value")]
HRESULT Value([out, retval] VARIANT *pVal);
[propput, helpstring("property Value")]
HRESULT Value([in] VARIANT newVal);
};
As you can see ICell
interface is designed to manipulate one cell. A cell has a Value
property that is used to read or set the value of the cell.
IRow
[
helpstring("IRow interface used to access rows from grid"),
uuid(E18D7A91-E639-11d3-8138-0000B49DBD2E),
pointer_default(unique)
]
interface IRow : IUnknown
{
[propget, helpstring("Get the specified cell of this row")]
HRESULT Cell(long nIndex, [out, retval] ICell **pVal);
[propget, helpstring("Returs TRUE if this row is active and false otherwise")]
HRESULT Active([out, retval] BOOL *pVal);
[propput, helpstring("Put this property TRUE if you wanna activate this row")]
HRESULT Active([in] BOOL newVal);
[propget, helpstring("TRUE if this row is selected")]
HRESULT Selected([out, retval] BOOL *pVal);
[propput, helpstring("Put this property on TRUE if you want to select this row")]
HRESULT Selected([in] BOOL newVal);
[propget, helpstring("Returns the index of this row")]
HRESULT Index([out, retval] long *pVal);
};
IRow
interface can be used to manipulate each row. A row is composed from cells. You can get a reference to a cell from a given row using the column index. Only one row can be active at a time. This row can be retrieved or changed through Active
property. One or more rows can be selected. You can select or find if a row is selected through Selected
property. The index of a row is accessible for reading by Index
property.
IColumn
[
helpstring("IColumn interface used to access columns individually"),
uuid(506580C1-E643-11d3-8138-0000B49DBD2E),
pointer_default(unique)
]
interface IColumn : IUnknown
{
[propget, helpstring("Access a cell from this column")]
HRESULT Cell(long nIndex, [out, retval] ICell** pVal);
[propget, helpstring("Get the width of the column in pixels")]
HRESULT Width([out, retval] long *pVal);
[propput, helpstring("Put the width of the column in pixels")]
HRESULT Width([in] long newVal);
[propget, helpstring("Get the label of the column")]
HRESULT Name([out, retval] BSTR *pVal);
[propput, helpstring("Put the label of the column")]
HRESULT Name([in] BSTR newVal);
[propget, helpstring("Get data type used for this column")]
HRESULT DataType([out, retval] DataType *pVal);
[propget, helpstring("Put the values range")]
HRESULT Range([out, retval] BSTR *pVal);
[propput, helpstring("Get the values range")]
HRESULT Range([in] BSTR newVal);
[propget, helpstring("Get the index of the column")]
HRESULT Index([out, retval] long *pVal);
[propget, helpstring("Get the ReadOnly state of the column")]
HRESULT ReadOnly([out, retval] BOOL *pVal);
[propput, helpstring("Changes the ReadOnly state of the column")]
HRESULT ReadOnly([in] BOOL newVal);
[propget, helpstring("Get SortOrder property of this column")]
HRESULT SortOrder([out, retval] SortOrder *pVal);
[propput, helpstring("Changes the SortOrder property of the column")]
HRESULT SortOrder([in] SortOrder newVal);
[propget, helpstring("Returns AllowUserToSort value")]
HRESULT AllowUserToSort([out, retval] BOOL *pVal);
[propput, helpstring("Sets AllowUserToSort value")]
HRESULT AllowUserToSort([in] BOOL newVal);
[helpstring("Resorts the grid data, by this column content")]
HRESULT Resort();
};
IColumn
interface can be used to read or change properties of each column. Like for rows, you can access cells from a column through Cell
property if you specify index of the cell in column. A column has the following properties: Width
in pixels, Name
which is the label displayed on the top header, DataType
is read-only property used to retrieve data type associated with the column. Index
is the zero-based index of the column within the grid and ReadyOnly
is a boolean property which can be used to make a column available only for view. Even the grid is not entirely ReadOnly.
Events
There are a few events that the grid raises when certain operations take place. These events can be seen in the following dispinterface:
[
uuid(855D88AF-C9BD-11D3-A34E-0080AD303A9A),
helpstring("_ISmartGridEvents Interface")
]
dispinterface _ISmartGridEvents
{
properties:
methods:
[id(1), helpstring("Raised when the active row has changed")]
HRESULT RowChanged(long nPrevious, long nNew);
[id(2), helpstring("Raised when the active column has changed")]
HRESULT ColumnChanged(long nPrevious, long nNew);
[id(3), helpstring("Raised when the active cell has changed")]
HRESULT CellChanged(long nPreviousRow, long nPreviousColumn,
long nNewRow, long nNewColumn);
[id(4), helpstring("Raised before edit begins")]
VARIANT_BOOL BeginCellEdit(long nRow, long nColumn, ICell*);
[id(5), helpstring("Raised after edit is ended")]
HRESULT EndCellEdit(long nRow, long nColumn, ICell*);
[id(6), helpstring("Raised after cell content is updated")]
HRESULT CellUpdated(long nRow, long nColumn, ICell*);
};
I have tested this DLL using Microsoft Visual Basic 6.0. The archive contains the VB project that I used to test this ActiveX control.
This control does not depends on MFC.
History
-
Date Posted: March 28, 2000
-
Updated: April 12, 2000
- Added
DataType_Option
and DataType_StrictOption
that are edited using combo boxes and drop down lists.
- Added "Range Specification" for values in each column. This will provide better control on values entered in grid's cells.
ReadOnly
attribute is available on column and on entire grid.
- When adding a row or a column, we can optionally receive a reference to the added object ( row or column ) so that we can immediately change its properties.
- Added some event raising.
- Bug fixes.
-
Updated: April 28, 2000
- Added sorting capabilities. Double clicking on a column's header sorts the content of the grid after that column ( if the sorting is allowed for the column ). F5 key can be used for resort.
- Bug fixes.
-
Updated: May 16, 2000
- Added three events:
BeginCellEdit
, EndCellEdit
and CellUpdated
.
- Bug fixes ( including those reported in comments till May 16 2000 ).
-
Updated: May 18, 2000
- Added event
ValueNotInRange
, which is raised when the user enters a value that it is not in specified range.
- Bug fixes.