property Items.SelectItem(Item as HITEM) as Boolean

Selects or unselects a specific item.

TypeDescription
Item as HITEM A long expression that indicates the item's handle being selected.
Boolean A boolean expression that indicates the item's state. True if the item is selected, and False if the item is not selected.

Use the SelectItem property to programmatically select an item. The SelectItem property indicates whether an item is selected or not selected, giving its handle. Use the SelectedItem property to get the selected items, giving their indexes. The control fires SelectionChanged event when the user changes the selection. The SelectCount property counts the selected items in the control, when the control supports multiple selection. Use the SingleSel property to specify whether the control supports single or multiple selection. If the SingleSel property is True, the user can select a single item only. Use the FullRowSelect property to specify how the user can select the cells or items using the mouse. Use the SelectColumnIndex or SelectColumInner property to retrieve the index of selected column, and the index of the inner cell being selected. The FocusItem property specifies the handle of the focused item. The control can have only a single focused item. If the control supports single selection, the FocusItem property gets the selected item too. Use the EnsureVisibleItem property to ensure that an item is visible. Use the SelBackColor property to indicate the background color for selected items. Use the SelForeColor property to specify the foreground color for selected items. The SelectPos property selects/unselects items by position. The Selection property selects/unselects items by index.

The following VB sample selects the first created item: Grid1.Items.SelectItem(Grid1.Items(0)) = True

The following VB sample displays the selected item from the cursor ( SingleSel property is True, and the control contains NO inner cells, SplitCell method   ):

Private Sub Grid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim h As HITEM, c As Long, hit As HitTestInfoEnum
    With Grid1
        h = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, hit)
        If Not h = 0 Then
            If (.Items.SelectItem(h)) Then
                Debug.Print "The '" & .Items.CellCaption(h, c) & "' item is selected"
            End If
        End If
    End With
End Sub

The following VB sample displays the selected item from the cursor, ( SingleSel property is True, and the control contains inner cells, SplitCell method )

Private Sub Grid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim h As HITEM, c As Long, hit As HitTestInfoEnum
    With Grid1
        h = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, hit)
        If Not h = 0 Or Not c = 0 Then
            If h = 0 Then
                h = .Items.CellItem(.Items.CellParent(h, c))
            End If
            If (.Items.SelectItem(h)) Then
                Debug.Print "The '" & .Items.CellCaption(h, c) & "' item is selected"
            End If
        End If
    End With
End Sub

The following VB sample selects the item as user moves the cursor ( SingleSel property is True, and the control contains NO inner cells, SplitCell method ):

Private Sub Grid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim h As HITEM, c As Long, hit As HitTestInfoEnum
    With Grid1
        h = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, hit)
        If Not h = 0 Then
            .Items.SelectItem(h) = True
        End If
    End With
End Sub

The following VB sample selects the item as user moves the cursor ( SingleSel property is True, and the control contains inner cells, SplitCell method ):

Private Sub Grid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim h As HITEM, c As Long, hit As HitTestInfoEnum
    With Grid1
        h = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, hit)
        If Not h = 0 Or Not c = 0 Then
            If h = 0 Then
                h = .Items.CellItem(.Items.CellParent(h, c))
            End If
            .Items.SelectItem(h) = True
        End If
    End With
End Sub

The following VB sample selects the first visible item:

With Grid1.Items
    .SelectItem(.FirstVisibleItem) = True
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 selects the first visible item:

#include "Items.h"
CItems items = m_grid.GetItems();
items.SetSelectItem( items.GetFirstVisibleItem(), TRUE );

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 selects the first visible item:

With AxGrid1.Items
    .SelectItem(.FirstVisibleItem) = True
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 selects the first visible item:

axGrid1.Items.set_SelectItem(axGrid1.Items.FirstVisibleItem, true);

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 selects the first visible item:

with thisform.Grid1.Items
	.DefaultItem = .FirstVisibleItem
	.SelectItem(0) = .t.
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