Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The Ultimate Grid Datasources

0.00/5 (No votes)
25 Aug 2007 1  
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.

Contents

Introduction

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.

Serialization

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:

ADO CADOImpDatasource

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

Files:
DataSources\ADO\ADOImpDatasource.cpp ADOImpDatasource.h

Samples:
DataSources\ADO\ADODataSource\
DataSources\ADO\ADO Sample\

Array CUGCBArrayDataSource

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.

Files:
DataSources\Array\UGCBADSC.cpp UGCBADSC.h

Sample:
DataSources\Array\Dumper3\

DAO CUGDAODataSource

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.

Files:
DataSources\DAO\Ugdao.cpp Ugdao.h

Samples:
DataSources\DAO\DAO Sorting\
DataSources\DAO\MyAccess\

Additional:
DataSources\DAO\dao.htm
Delimited CUG_Delimitted

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

Files:
DataSources\Delimited\UGdelim.cpp UGdelim.h

Sample:
DataSources\Delimited\Example\

Additional:
DataSources\delimited\ReadME.txt

Html CUG_HTML_DataSource

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.

Files:
DataSources\Html\UGHTML.cpp UGHTML.h

Sample:
DataSources\Html\htmlexam\

ODBC CUGODBCDatasource

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.

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

Samples:
DataSources\ODBC\odbcexam\
DataSources\ODBC\odbcexm2\

OLE DB CUG_OLEDB_DataSource

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

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

Sample:
DataSources\OleDB\Example\

XML CXMLParser

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.

Files:
DataSources\XML\COXParser\*.*
DataSources\XML\XMLParser.cpp XMLParser.h

Sample:
DataSources\XML\XMLSample\

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.

History

Initial CodeProject release August 2007.

License

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