Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / MFC
Article

The Ultimate Grid Datasources

Rate me:
Please Sign up or sign in to vote.
4.56/5 (7 votes)
24 Aug 2007CPOL5 min read 88.7K   33   24
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:

ADOCADOImpDatasource

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\

ArrayCUGCBArrayDataSource

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\

DAOCUGDAODataSource

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

HtmlCUG_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\

ODBCCUGODBCDatasource

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 DBCUG_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\

XMLCXMLParser

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, 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.
This is a Organisation

476 members

Comments and Discussions

 
QuestionData Source creation Pin
Bill Lea12-Dec-16 11:42
Bill 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
gfanini@tiscalinet.it7-Aug-13 5:53 
BugCannot download the source code for this project Pin
Wouter Vos23-Nov-12 4:18
Wouter 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
maplewang10-Jul-12 21:31 
GeneralMy vote of 5 Pin
maplewang10-Jul-12 21:25
maplewang10-Jul-12 21:25 
QuestionHow to set top header rows number and join header cells in DataSource? Pin
iceway16-Nov-09 17:39
iceway16-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
Tim Deveaux17-Nov-09 8:48 
GeneralCUGDatasource::Browse crash [modified] Pin
Rymka5-Nov-09 3:10
Rymka5-Nov-09 3:10 
GeneralDemo program "db7" (odbcexam2) crashes Pin
JRCooper27-Oct-09 9:14
JRCooper27-Oct-09 9:14 
QuestionHow to use a dynamical data source with a known row count Pin
iceway26-Oct-09 18:17
iceway26-Oct-09 18:17 
AnswerRe: How to use a dynamical data source with a known row count Pin
Tim Deveaux27-Oct-09 8:12
Tim Deveaux27-Oct-09 8:12 
GeneralCursors: clientside / serverside Pin
mssg13-Feb-09 2:27
mssg13-Feb-09 2:27 
QuestionUsing .mdf files in Ultimate Grid? Pin
vahid_m_20089-Dec-08 23:54
vahid_m_20089-Dec-08 23:54 
AnswerRe: Using .mdf files in Ultimate Grid? Pin
Tim Deveaux10-Dec-08 6:15
Tim Deveaux10-Dec-08 6:15 
GeneralRe: Using .mdf files in Ultimate Grid? Pin
vahid_m_200815-Dec-08 23:48
vahid_m_200815-Dec-08 23:48 
GeneralRe: Using .mdf files in Ultimate Grid? Pin
Tim Deveaux17-Dec-08 6:20
Tim Deveaux17-Dec-08 6:20 
GeneralProblem with the Datasource based on SQL Server2000 Pin
jiangbing30-Nov-08 1:46
jiangbing30-Nov-08 1:46 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux30-Nov-08 7:48
Tim Deveaux30-Nov-08 7:48 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
jiangbing30-Nov-08 15:07
jiangbing30-Nov-08 15:07 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux2-Dec-08 9:24
Tim Deveaux2-Dec-08 9:24 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
jiangbing4-Dec-08 3:18
jiangbing4-Dec-08 3:18 
GeneralRe: Problem with the Datasource based on SQL Server2000 Pin
Tim Deveaux4-Dec-08 6:40
Tim Deveaux4-Dec-08 6:40 
QuestionDatasource based on Oracle OCI Pin
mgampi29-Aug-07 1:32
mgampi29-Aug-07 1:32 
AnswerRe: Datasource based on Oracle OCI Pin
pengdingfu26-Nov-07 17:05
pengdingfu26-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.