property Items.RootItem ([Position as Long]) as HITEM

Retrieves the handle of the root item given its index in the root items collection.

TypeDescription
Position as Long A long value that indicates the index of the root item.
HITEM A long expression that indicates the handle of the root item. 

A root item is an item that has no parent (ItemParent() = 0). Use the RootCount property of to count the root items. Use the AddItem to add root items to the control. Use the InsertItem method to insert child items. Use the FirstVisibleItem property to get the first visible item in the control's client area. The NextVisibleItem property retrieves the handle of next visible item. The NextSiblingItem property retrieves the next sibling of the item in the parent's child list. Use the RootItem property to get the first visible item in the list. If you need to enumerate the items as they are added, you may use the ItemByIndex property. 

The following VB sample enumerates all root items. This sample can be used to list all your items as they are displayed ( sorted ), when the control displays a plain list of items. A plain list of items is composed by items that do not have child items.

Dim i As Long, n As Long
With Grid1.Items
    n = .RootCount
    For i = 0 To n - 1
        Debug.Print .CellValue(.RootItem(i), 0)
    Next
End With

The following VB sample enumerates all cells in the control, as they are listed:

With Grid1
    Dim nCols As Long
    nCols = .Columns.Count
    With .Items
        Dim h As HITEM
        h = .RootItem(0)
        While Not h = 0
            Dim i As Long
            For i = 0 To nCols - 1
                Debug.Print .CellValue(h, i)
            Next
            h = .NextVisibleItem(h)
        Wend
    End With
End With

The following C++ sample enumerates all root items:

#include "Items.h"
CItems items = m_grid.GetItems();
for ( long i = 0 ; i < items.GetRootCount(); i++ )
{
	COleVariant vtItem( items.GetRootItem(i) ), vtColumn( long(0) );
	OutputDebugString( V2S( &items.GetCellValue( vtItem, vtColumn ) ) );
}

where the V2S function converts a VARIANT expression to a string expression and looks like:

static CString V2S( VARIANT* pv, LPCTSTR szDefault = _T("") )
{
	if ( pv )
	{
		if ( pv->vt == VT_ERROR )
			return szDefault;

		COleVariant vt;
		vt.ChangeType( VT_BSTR, pv );
		return V_BSTR( &vt );
	}
	return szDefault;
}

if you are using MFC, or

static string V2S( VARIANT* pv, LPCTSTR szDefault = _T("") )
{
	if ( pv )
	{
		if ( pv->vt == VT_ERROR )
			return szDefault;

		CComVariant vt;
		if ( SUCCEEDED( vt.ChangeType( VT_BSTR, pv ) ) )
		{
			USES_CONVERSION;
			return OLE2T(V_BSTR( &vt ));
		}
	}
	return szDefault;
}

if you are using STL.

The following VB.NET sample enumerates all root items:

With AxGrid1.Items
    Dim i As Integer
    For i = 0 To .RootCount - 1
        Debug.Print(.CellValue(.RootItem(i), 0))
    Next
End With

The following C# sample enumerates all root items:

for (int i = 0; i < axGrid1.Items.RootCount; i++)
{
	object strValue = axGrid1.Items.get_CellValue(axGrid1.Items.get_RootItem(i), 0);
	System.Diagnostics.Debug.WriteLine(strValue != null ? strValue.ToString() : "");
}

The following VFP sample enumerates all root items:

with thisform.Grid1.Items
	local i
	for i = 0 to .RootCount - 1
		.DefaultItem = .RootItem(i)
		wait window nowait .CellValue(0,0)
	next
endwith