Click here to Skip to main content
11,924,995 members (52,852 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked

Smart Grid

, 24 May 2000
Rate this:
Please Sign up or sign in to vote.
Build a grid using ATL, STL and Win32 API.


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.

Smart Grid

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

Smart Grid

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.

Smart Grid

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.

Smart Grid

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.

// This are data source types
typedef [v1_enum] enum
   DataSourceType_None = 0,
} 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.


// Use this to manipulate grid properties
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.


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


   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);
  [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.


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


There are a few events that the grid raises when certain operations take place. These 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("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.


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Turc
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionData Update? Pin
Member 450900314-Dec-13 5:26
memberMember 450900314-Dec-13 5:26 
QuestionGridView Pin
naveendhalay6-Jun-12 22:39
membernaveendhalay6-Jun-12 22:39 
GeneralReal Numbers Pin
Dima24122-Jun-08 11:59
memberDima24122-Jun-08 11:59 
Generalgood, but could be better with mouse actions Pin
Unruled Boy22-Sep-06 3:14
memberUnruled Boy22-Sep-06 3:14 
GeneralBuggy Pin
Dave Cross26-Oct-05 6:25
memberDave Cross26-Oct-05 6:25 
QuestionHow to use Smart Grid in VC++ Pin
tiniv21-Apr-05 0:59
membertiniv21-Apr-05 0:59 
GeneralCrash upon dropdown change Pin
Martijn27-Nov-02 3:42
memberMartijn27-Nov-02 3:42 
GeneralRe: Crash upon dropdown change Pin 14:21 14:21 
GeneralRe: Crash upon dropdown change Pin
ihope4-Jul-05 21:24
memberihope4-Jul-05 21:24 
GeneralArabic bug Pin
scis12-Oct-02 3:45
sussscis12-Oct-02 3:45 
GeneralAnother subtle and critical bug Pin
Hemant Kurdia7-Sep-02 1:01
memberHemant Kurdia7-Sep-02 1:01 
GeneralSort List view by clicking Column header Pin
shee13-Jun-02 3:14
membershee13-Jun-02 3:14 
Questionwho can send me a sample used by vc++? Pin
yangpu27-Mar-02 22:20
memberyangpu27-Mar-02 22:20 
AnswerRe: who can send me a sample used by vc++? Pin
Michief12-Mar-03 3:14
memberMichief12-Mar-03 3:14 
GeneralRe: who can send me a sample used by vc++? Pin
seawolfxu8-Aug-04 2:49
memberseawolfxu8-Aug-04 2:49 
Generalnewbie question concerning VC++ / MFC SDI-app Pin
glenn3-Mar-02 10:04
memberglenn3-Mar-02 10:04 
GeneralRe: newbie question concerning VC++ / MFC SDI-app Pin
Christian Graus3-Mar-02 10:18
memberChristian Graus3-Mar-02 10:18 
GeneralProblem when using VC++ client Pin
Hemant Kurdia27-Jan-02 20:14
memberHemant Kurdia27-Jan-02 20:14 
GeneralAccessing a cell in smartgrid Pin
Leena15-Dec-01 3:14
memberLeena15-Dec-01 3:14 
GeneralAccessing a cell in smartgrid Pin
Leena15-Dec-01 3:10
memberLeena15-Dec-01 3:10 
GeneralOne new bug. Pin
Michail5-Dec-01 4:48
memberMichail5-Dec-01 4:48 
GeneralThis is cool Pin
<marquee>Srikanth</marquee>20-Nov-01 22:42
memberSrikanth20-Nov-01 22:42 
Generalsmall problem with color setting Pin
Anonymous12-Nov-01 3:47
memberAnonymous12-Nov-01 3:47 
Questionsort bug? Pin
ycmag23-Oct-01 23:08
memberycmag23-Oct-01 23:08 
QuestionATL C++ sample using the grid? Pin
AllanC22-Oct-01 18:48
memberAllanC22-Oct-01 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 25 May 2000
Article Copyright 2000 by Alex Turc
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid