property Items.SelectedItem ([Index as Long]) as HITEM

Retrieves the selected item's handle given its index in the selected items collection.

 TypeDescription 
   Index as Long Identifies the index of the selected item into selected items collection. if it is missing, 0 is used.  
   HITEM A long expression that indicates the handle of the selected item.  

The SelectedItem property gets the handle of the items being selected. If the control supports multiple selection, you can use the SelectCount property to find out how many items are selected in the control. Use the SingleSel property to enable single or multiple selection. If the control supports single selection only a single item can be selected at runtime. Use the SingleSel property to specify whether the control supports single or multiple selection. If the control supports single selection, the FocusItem and SelectedItem property gets the handle of the selected/focused item, that's the same. Use the SelectItem property to specify whether an item is selected or not.  The control fires the SelectionChanged event when user changes the selection in the control. Use the SelForeColor and SelBackColor properties to specify colors for selected items. Use the SelectableItem property to specify whether the user can select an item.

The following VB sample prints the value of the selected/focused cell: Debug.Print Grid1.Items.CellValue(Grid1.Items.FocusItem(), 0).  

The following VB sample draws italic the selected item, when selection is changed:

Private Sub Grid1_SelectionChanged()
    If Not (h = 0) Then Grid1.Items.ItemItalic(h) = False
    h = Grid1.Items.SelectedItem()
    Grid1.Items.ItemItalic(h) = True
End Sub

The following VB sample enumerates all selected items in the control:

Dim i As Long, j As Long, nCols As Long, nSels As Long
nCols = Grid1.Columns.Count
With Grid1.Items
    nSels = .SelectCount
    For i = 0 To nSels - 1
        Dim s As String
        For j = 0 To nCols - 1
            s = s + .CellValue(.SelectedItem(i), j) + Chr(9)
        Next
        Debug.Print s
    Next
End With

The following VB sample unselects all items in the control:

With Grid1
    .BeginUpdate
    With .Items
        While Not .SelectCount = 0
            .SelectItem(.SelectedItem(0)) = False
        Wend
    End With
    .EndUpdate
End With

The following C++ sample enumerates the selected items:

CItems items = m_grid.GetItems();
long n = items.GetSelectCount();
if ( n != 0 )
{
	for ( long i = 0; i < n; i++ )
	{
		long h = items.GetSelectedItem( i );
		COleVariant vtString;
		vtString.ChangeType( VT_BSTR, &items.GetCellValue( COleVariant( h ), COleVariant( (long)0 ) ) );
		CString str = V_BSTR( &vtString );
		MessageBox( str );
	}
}

The following C++ sample unselects all items in the control:

m_grid.BeginUpdate();
CItems items = m_grid.GetItems();
while ( items.GetSelectCount() )
	items.SetSelectItem( items.GetSelectedItem( 0 ), FALSE );
m_grid.EndUpdate();

The following VB.NET sample enumerates the selected items:

With AxGrid1.Items
    Dim nCols As Integer = AxGrid1.Columns.Count, i As Integer
    For i = 0 To .SelectCount - 1
        Debug.Print(.CellValue(.SelectedItem(i), 0))
    Next
End With

The following VB.NET sample unselects all items in the control:

With AxGrid1
    .BeginUpdate()
    With .Items
        While Not .SelectCount = 0
            .SelectItem(.SelectedItem(0)) = False
        End While
    End With
    .EndUpdate()
End With

The following C# sample enumerates the selected items:

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

The following C# sample unselects all items in the control:

axGrid1.BeginUpdate();
EXGRIDLib.Items items = axGrid1.Items;
while (items.SelectCount != 0)
	items.set_SelectItem(items.get_SelectedItem(0), false);
axGrid1.EndUpdate();

The following VFP sample enumerates the selected items:

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

The following VFP sample unselects all items in the control:

With thisform.Grid1
	.BeginUpdate()
	with .Items
		do while ( .SelectCount() # 0 )	
			.DefaultItem = .SelectedItem(0)
			.SelectItem(0) = .f.
		enddo
	endwith
	.EndUpdate()
EndWith

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