Click here to Skip to main content
14,097,639 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 24 Aug 2007
Licenced CPOL

The Ultimate Grid Datasources

, 24 Aug 2007
Rate this:
Please Sign up or sign in to vote.
An introduction to using custom datasources with the Ultimate Grid

Visit the Ultimate Grid main page for an overview and configuration guide to the Ultimate Grid library.



One of the nice design features of the Ultimate Grid is the abstraction of the data layer from the presentation layer through the use of a base data source class. The CUGDataSource class abstracts all functions relating to a data source. This design allows for a high degree of code reuse, and allows tremendous flexibility in Ultimate Grid's implementation, allowing many different types of data sources to be used in combination, or interchangeably, without the need to re-code the inner workings of the grid or your application.

Most developers will begin with the default - the 'built in' "Memory Manager" (CUGMem) data source class, which allows for cell objects to be stored in the grid for display, and retrieved for manipulation. CUGMem is derived from CUGDataSource.

Up to 64000 CUGDataSource derived classes may be attached to each CUGCtrl class.

Why Use a Custom DataSource?

Dynamic Loading of Cell Information

You may wish to use a derived CUGDataSource class for loading cell information dynamically. Dynamic loading of cell information greatly improves performance for large tables, reduces memory requirements, and allows the grid to be displayed on-screen instantaneously.


You may have a file which contains data with mark up to display and edit - XML would be a good example.

Incorporating Data Access APIs

Many Microsoft and 3rd party database implementations supply C/C++ interfaces to directly invoke SQL queries and access the result sets returned. This can allow for fully dynamic identification of columns, column types, and an efficient dynamic display of both static and dynamic result sets.

Pre-built DataSources and Examples

Many pre-defined CUGDataSource objects are included with Ultimate Grid:


This datasource wraps the Active Data Objects interface, which in turn layers OLE DB.

DataSources\ADO\ADOImpDatasource.cpp ADOImpDatasource.h

DataSources\ADO\ADO Sample\


An example of using a simple byte array as a datasource - the Dumper sample loads a file as a binary array and displays a hex/text dump.

DataSources\Array\UGCBADSC.cpp UGCBADSC.h



The DAO datasource maintains member CDaoDatabase, CDaoTableDef, CDaoQueryDef, CDaoRecordset, and CDaoFieldInfo objects to allow for dynamic loading of Microsoft Access database query result sets.

Support for transactions is available.

Implements sorting and find next (FindDialog) functionality.

DataSources\DAO\Ugdao.cpp Ugdao.h

DataSources\DAO\DAO Sorting\


An example of a serializable datasource, the CUG_Delimited class can read CSV files etc. The delimiter character can be specified. Derived from CUGMem (the default memory based datasource for the grid).

DataSources\Delimited\UGdelim.cpp UGdelim.h




This data source handles importing simple HTML tables. It will skip over embedded tables to completely import the table. You can decided which of the outer most tables to import by passing a table number to the Open function.

DataSources\Html\UGHTML.cpp UGHTML.h



The CUGODBCDatasource class includes a CUGRecordset class derived from CRecordset that uses the ODBC API directly to enable dynamic identification of table columns, types, etc.

DataSources\ODBC\ugrecset.cpp ugrecset.h UGODBC.CPP UGODBC.H



The OLE DB datasource and column binding classes can dynamically access database result sets through the OLE DB data interfaces.

DataSources\OleDB\binds.cpp binds.h ugoledb.cpp ugoledb.h



The XML datasource atypical of the classes described here, as is not derived from CUGDataSource. The CXMLParser class maintains a pointer to a grid object which it can populate with data and cell properties gleaned from an XML file, but the grid itself uses the default memory datasource.

This class has been updated for better rendering of Microsoft Excel xml attributes.

DataSources\XML\XMLParser.cpp XMLParser.h


Creating Custom CUGDataSource Classes

Probably the most common need for a custom datasource is when dealing with a large amount of external data (for which you already have an interface) that can retrieve data based on a col and row vector, obviating the overhead that would be required in duplicating data items by preloading them through the SetCell interface of the grid.

Custom data sources can be engineered with as little as one virtual override of the GetCell() function. Depending upon your requirements, you may also wish to provide the data source class with the ability to write information back to the data source through the SetCell() function. Your data source class can also be responsible for reordering the data source based upon indices, calculating the total number of rows, calculating the number columns, determining the column headings, etc. You may provide as much or as little functionality as you require in your custom data source classes.

In a fully developed data source class special functions such as SetGridFromDataSource() can be added to allow the grid to automatically ask the data source for information on column titles, column types, formatting, etc., and automatically set the default grid and column cell objects with this information.

Once you have a CUGDatasource derived class in place, simply declare an instance in and call the grid's AddDatasource method to add it to the grid's array of known datasources. To set it as the current datasource for the grid, call SetDefDatasource with the index returned from AddDatasource. The grid will then query your GetCell method when drawing data.

For more on creating and using datasources with the Ultimate Grid, refer to the tutorials and class references in the compiled CHM help documentation.


Initial CodeProject release August 2007.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

461 members

You may also be interested in...

Comments and Discussions

QuestionData Source creation Pin
Bill Lea12-Dec-16 11:42
memberBill Lea12-Dec-16 11:42 
Questionhow to force redraw of cell if visible after external datasource has updated Pin
gfanini@tiscalinet.it7-Aug-13 5:53
membergfanini@tiscalinet.it7-Aug-13 5:53 
BugCannot download the source code for this project Pin
Wouter Vos23-Nov-12 4:18
memberWouter Vos23-Nov-12 4:18 
QuestionHow to make ID column not show and insert can generate a new ID value Pin
maplewang10-Jul-12 21:31
membermaplewang10-Jul-12 21:31 
GeneralMy vote of 5 Pin
maplewang10-Jul-12 21:25
membermaplewang10-Jul-12 21:25 
QuestionHow to set top header rows number and join header cells in DataSource? Pin
iceway16-Nov-09 17:39
membericeway16-Nov-09 17:39 
AnswerRe: How to set top header rows number and join header cells in DataSource? Pin
Tim Deveaux17-Nov-09 8:48
memberTim Deveaux17-Nov-09 8:48 
GeneralCUGDatasource::Browse crash [modified] Pin
Rymka5-Nov-09 3:10
memberRymka5-Nov-09 3:10 
GeneralDemo program "db7" (odbcexam2) crashes Pin
JRCooper27-Oct-09 9:14
memberJRCooper27-Oct-09 9:14 
QuestionHow to use a dynamical data source with a known row count Pin
iceway26-Oct-09 18:17
membericeway26-Oct-09 18:17 
AnswerRe: How to use a dynamical data source with a known row count Pin
Tim Deveaux27-Oct-09 8:12
memberTim Deveaux27-Oct-09 8:12 
GeneralCursors: clientside / serverside Pin
mssg13-Feb-09 2:27
membermssg13-Feb-09 2:27 
QuestionUsing .mdf files in Ultimate Grid? Pin
vahid_m_20089-Dec-08 23:54
membervahid_m_20089-Dec-08 23:54 
AnswerRe: Using .mdf files in Ultimate Grid? Pin
Tim Deveaux10-Dec-08 6:15
memberTim Deveaux10-Dec-08 6:15 
GeneralRe: Using .mdf files in Ultimate Grid? Pin
vahid_m_200815-Dec-08 23:48
membervahid_m_200815-Dec-08 23:48 
GeneralRe: Using .mdf files in Ultimate Grid? Pin
Tim Deveaux17-Dec-08 6:20
memberTim Deveaux17-Dec-08 6:20 
GeneralProblem with the Datasource based on SQL Server2000 Pin
jiangbing30-Nov-08 1:46
memberjiangbing30-Nov-08 1:46 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux30-Nov-08 7:48
memberTim Deveaux30-Nov-08 7:48 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
jiangbing30-Nov-08 15:07
memberjiangbing30-Nov-08 15:07 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux2-Dec-08 9:24
memberTim Deveaux2-Dec-08 9:24 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
jiangbing4-Dec-08 3:18
memberjiangbing4-Dec-08 3:18 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux4-Dec-08 6:40
memberTim Deveaux4-Dec-08 6:40 
QuestionDatasource based on Oracle OCI Pin
mgampi29-Aug-07 1:32
membermgampi29-Aug-07 1:32 
AnswerRe: Datasource based on Oracle OCI Pin
pengdingfu26-Nov-07 17:05
memberpengdingfu26-Nov-07 17:05 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190518.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid