![]() | Type | Description | ||
| Items as Variant | An array that control uses to fill with | |||
| Index as Variant | Reserved. |
The PutItems method loads an array of of integer, long, date, string, double, float, or variant elements. The PutItems verifies the range of the passed array, and if number of column in the array doesn't match the number of columns in the control, it fails. The error message provided in this case specifies the number of columns that the control should have in order to load the array. The PutItems method doesn't clear the Items collection. It appends to the end of the list, each element of the array. The PutItems method fires AddItem event for each item added to Items collection. Use the Items property to access the items collection. Use the GetItems method to get the collection of items into a safe array. 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 loads few items using the VB Array function:
With Grid1
.BeginUpdate
.Columns.Add "Column 1"
.PutItems Array("Item 1", "Item 2", "Item 3")
.EndUpdate
End WithThe following VB sample loads a simple array of strings:
Dim v(2) As String
v(0) = "First"
v(1) = "Second"
v(2) = "Third"
With Grid1
.BeginUpdate
.Columns.Add "Strings"
.PutItems v
.EndUpdate
End With
The following VB sample loads a two dimensional array of longs:
Dim v(1, 3) As Long
v(0, 0) = 0
v(0, 1) = 1
v(0, 2) = 2
v(0, 3) = 3
v(1, 0) = 4
v(1, 1) = 5
v(1, 2) = 6
v(1, 3) = 7
With Grid1
.BeginUpdate
.Columns.Add "0"
.Columns.Add "1"
.PutItems v
.EndUpdate
End With
The following VB sample loads the collection of records from an ADO recordset:
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
.ColumnAutoResize = False
.MarkSearchColumn = False
.DrawGridLines = True
' Adds a column for eac field found
With .Columns
Dim f As Object
For Each f In rs.Fields
.Add f.Name
Next
End With
' Loads the collection of records
.PutItems rs.GetRows()
'Changes the editor of the "Photo" column
.Columns("Photo").Editor.EditType = PictureType
.EndUpdate
End With
The following C++ sample loads records from an ADO recordset, using the PutItems method:
#include "Items.h"
#include "Columns.h"
#include "Column.h"
#pragma warning( disable : 4146 )
#import <msado15.dll> rename ( "EOF", "adoEOF" )
using namespace ADODB;
_RecordsetPtr 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 );
CColumns columns = m_grid.GetColumns();
for ( long i = 0; i < spRecordset->Fields->Count; i++ )
columns.Add( spRecordset->Fields->GetItem(i)->Name );
COleVariant vtMissing; V_VT( &vtMissing ) = VT_ERROR;
m_grid.PutItems( &spRecordset->GetRows(-1), vtMissing );
m_grid.EndUpdate();
}
}
catch ( _com_error& e )
{
AfxMessageBox( e.Description() );
}
}
The sample uses the #import statement to import ADODB recordset's type library. The sample enumerates the fields in the recordset and adds a new column for each field found. Also, the sample uses the GetRows method of the ADODB recordset to retrieves multiple records of a Recordset object into a safe array. Please consult the ADODB documentation for the GetRows property specification.