property Grid.DataSource as Object
Retrieves or sets a value that indicates the data source for the object.

 TypeDescription 
   Object An Object that defines the control's data. Currently, the control accepts ADO.Recordset, ADODB.Recordset objects, DAO recordsets  

The DataSource property binds the control to an ADO, ADODB or DAO recordset. Setting the DataSource property clears the control's columns collection.  The DataSource property adds a column for each field find in the recordset. Depending on type of the field, the control sets the column's EditType property. For instance, an field of "OLE Object" type is converted to PictureType edit type, a field of Date type, uses DateType edit type. The DetectAddNew property detects adding new records to a recordset. The DetectDelete property detects removing records from the recordset.

The DataSource property can load all data in the memory or just visible records ( virtual mode ). The VirtualMode property indicates whether the control loads all records in memory or just visible records. If the VirtualMode property is False ( by default ), all records are loaded in memory. The user must call the VirtualMode property before setting the DataSource, else an error occurs. If the VirtualMode property is True, before specifying the DataSource, the control loads virtually the records, just visible records are loaded. Use the control's virtual mode when you require to display and edit large databases, and you don't want to load the entire database in memory. Aldo, running the virtual mode disables some features including sorting and filtering, like explained in the VirtualMode property. The control builds an internal object that implements the IUnboundHandler interface that provides data for the control, when running in virtual mode, so the UnboundHandler property is not empty.

The following template script loads virtually the Order table, using the Template feature of the control ( copy the following template and paste it to the control's WYSWYG Template editor ) ( the sample uses Jet.OLEDB provider to handle MDB files ) :

Dim rs
VirtualMode = True
ColumnAutoResize = False
rs = CreateObject("ADOR.Recordset")
{
	Open("Orders","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Exontrol\ExGrid\Sample\SAMPLE.MDB", 3, 3 )
}
DataSource = rs
ConditionalFormats
{
	Add("%1 > 4").Bold = True
	Add("%1 = 1 or %1 = 3")
	{
		Underline = True
		ForeColor = RGB(255,0,0)
		ApplyTo = 1
	}
}

or ( the sample uses VFPOLEDB provider to handle DBF files )

Dim rs
VirtualMode = True
ColumnAutoResize = False
rs = CreateObject("ADODB.Recordset")
{
	Open("Select * from Students","Provider=vfpoledb;Data Source=D:\Program Files\Microsoft Visual Studio\Vfp98\Wizards\Template\Students And Classes\Data\STUDENTS AND CLASSES.DBC;Collating Sequence=machine"1,1 )
}
DataSource = rs

If the VirtualMode property is False, the control updates automatically the item associated to the record in the control, if a change occurs in the current record. The control doesn't update the Items collection if the records are deleted or added to the table, while the DetectDelete and DetectAddNew properties are False. 

Use the ConditionalFormats method to apply formats to a cell or range of cells, and have that formatting change depending on the value of the cell or the value of a formula.

The following VB sample binds the "Employees" table in NWIND database to your control:

Dim rs As Object
Const dwProvider = "Microsoft.Jet.OLEDB.4.0" ' OLE Data provider
Const nCursorType = 3 ' adOpenStatic
Const nLockType = 3 ' adLockOptimistic
Const nOptions = 2 ' adCmdTable
Const strDatabase = "D:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB"

'Creates an recordset and opens the "Employees" table, from NWIND database
Set rs = CreateObject("ADODB.Recordset")
rs.Open "Employees", "Provider=" & dwProvider & ";Data Source= " & strDatabase, nCursorType, nLockType, nOptions
With Grid1
    .BeginUpdate
        .AutoEdit = True
        .ColumnAutoResize = False
        .MarkSearchColumn = False
        .DrawGridLines = True
        Set .DataSource = rs
    .EndUpdate
End With

The following sample releases the data source that was previously bounded to the control

Set .DataSource = Nothing

The following C++ sample binds a table to the control:

#include "Items.h"
#include "Columns.h"
#include "Column.h"

#pragma warning( disable : 4146 )
#import <msado15.dll> rename ( "EOF", "adoEOF" )
using namespace ADODB;

_Recordset21Ptr spRecordset;
if ( SUCCEEDED( spRecordset.CreateInstance( "ADODB.Recordset") ) )
{
	// Builds the connection string.
	CString strTableName = "Employees", strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
	CString strPath = "D:\\Program Files\\Microsoft Visual Studio\\VB98\\NWIND.MDB";
	strConnection += strPath;
	try
	{
		// Loads the table
		if ( SUCCEEDED( spRecordset->Open(_variant_t( (LPCTSTR)strTableName ), _variant_t((LPCTSTR)strConnection), adOpenStatic, adLockPessimistic, NULL ) ) )
		{
			m_grid.BeginUpdate();
			m_grid.SetColumnAutoResize( FALSE );
			m_grid.SetDataSource( spRecordset );
			m_grid.EndUpdate();
		}
	}
	catch ( _com_error& e )
	{
		AfxMessageBox( e.Description() );
	}
}

The #import statement imports definitions for ADODB type library, that's used to fill the control.

 


Send comments on this topic.
1999-2017 Exontrol.COM, Software. All rights reserved.