Smart Grid

Environment: Windows NT4 (SP6), Visual C++ 6 (SP2)


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.

Click here for larger image

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:

// These are the data types
typedef [v1_enum] enum DataType
 DataType_Empty = 0,      // Not specified
 DataType_Null = 1,       // NULL
 DataType_I2 = 2,         // 2-byte signed int
 DataType_I4 = 3,         // 4-byte signed int
 DataType_R4 = 4,         // 4-byte real
 DataType_R8 = 5,         // 8-byte real
 DataType_Cy = 6,         // Currency
 DataType_Date = 7,       // Date
 DataType_BStr = 8,       // Binary string
 DataType_Dispatch = 9,   // IDispatch FAR*
 DataType_Error = 10,     // Scodes
 DataType_Bool = 11,      // Boolean; True=-1, False=0
 DataType_Variant = 12,   // VARIANT FAR*
 DataType_Unknown = 13,   // IUnknown FAR*
 DataType_UI1 = 17,       // Unsigned char	

// My types
DataType_Option = 0x0100, // Combo box
DataType_StrictOption     // Drop down list

} DataType;

As you 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
Strict Option. Data of this 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 posibilities 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 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 becoms 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 colours 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 or a connection
    string and a SQL SELECT statement. Unfortunately, in this version
    of the grid the databases are not automaticaly updated when the values
    of the cells change. This will be done in the next version.

    // This are data source types
    typedef [v1_enum] enum
     DataSourceType_None = 0,
    } DataSourceType;

    Database connection is not very well implemented yet, so I recommand this grid
    to be used with DataSourceType_None.

    Grid manipulation

    I will describe now the interfaces that can be used to manipulate grid elements.


    // Use this to manipulate grid properties
    interace ISmartGrid : IDispatch;

    ISmartGrid interface is used to manipulate general poperties of the grid.
    ISmartGrid is a big interface so I will describe its methods and properties
    whitout inserting the IDL code here.


    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 )


    // State
    // Data access
    // User interface

    Using methods from ISmartGrid interface can be added columns and rows to the grid.
    When adding a column or a row, a reference to it is optionally returned, in order to be used immediatelly after
    it is inserted. I split the properties of the grid in 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.


     helpstring("ICell interface is used to access cells"),
    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.


     helpstring("IRow interface used to access rows from grid"),
    interface IRow : IUnknown
     [propget, helpstring("Get the specified cell of this row")]
     HRESULT Cell(long nIndex, [out, retval] ICell **pVal);
     helpstring("Returs TRUE if this row is active and false otherwise")]
     HRESULT Active([out, retval] BOOL *pVal);
     helpstring("Put this property TRUE if you wanna activate this row")]
     HRESULT Active([in] BOOL newVal);
     helpstring("TRUE if this row is selected")]
     HRESULT Selected([out, retval] BOOL *pVal);
     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 retreived or change 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 accesible for reading by Index property.


     helpstring("IColumn interface used to access columns individually"),
    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 retreive data
    type associated with the column. Index is the zero-based index of the
    column within the grid and ReadyOnly boolean property which can be used
    to make a column available only for view, even the grid is not entirely ReadOnly.


    There are a few events that the grid raises when certain operations takes place. This events can be
    seen in the following dispinterface:

     helpstring("_ISmartGridEvents Interface")
    dispinterface _ISmartGridEvents
     [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);
     // Cell content changing events
     [id(4), helpstring("method BeginCellEdit")]
     VARIANT_BOOL BeginCellEdit(long nRow, long nColumn, ICell*);
     [id(5), helpstring("method EndCellEdit")]
     HRESULT EndCellEdit(long nRow, long nColumn, ICell*);
     [id(6), helpstring("method CellUpdated")]
     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.

    Version History

    • 2000/05/11 – Original Post
    • 2000/05/18 – Added a lot of functionality like sorting, combo-boxes,
      sorting capabilities, event firing and bug fixes.

    More by Author

    Must Read