How to get the selected row indexes from the grid window, or change selections programmatically?

Professional User Interface Suite, Copyright FOSS Software Inc. Help Published with Permission.

The selection model in the grid control is based on the array of rectangles. Each rectangle describes indices of left/top and right/bottom cells of a single rectangular selection region.

If your grid uses the single selection model, then the selection region consists of one rectangle only. The full-row and full-column selection models are also based on rectangles.

The data grid also supports two types of multiple selection models: excluded and non-excluded. The cell is assumed to be non-selected if it is covered by an even number of rectangles in the excluded model. The cell is assumed to be selected if it is covered by at least one rectangle in the non-excluded model.


Figure 1 Excluded and non-excluded selection

The excluded multiple selection model is turned on if the __EGBS_SUBTRACT_SEL_AREAS style is applied by invoking the SiwModifyStyle() method of the grid control. The SimpleGrids sample demonstrates both excluded and non-excluded models. Please note that each rectangle can be non-normalized, i.e. it is possible that left > right and/or top > bottom. You can enumerate all the selected cells in column nColNo by using:

LONG nRowNo = wndGrid.SelectionGetFirstRowInColumn( nColNo );
    while( nRowNo >= 0 )
    {
      . . .
      nRowNo = wndGrid.SelectionGetNextRowInColumn( nColNo, nRowNo );
    }

You can use the SelectionGetLastRowInColumn() and SelectionGetpPrevRowInColumn() methods for traversing the same rows in the reversed order from bottom to top.

You can also enumerate all the selected cells in row nRowNo by using:

LONG nColNo = wndGrid.SelectionGetFirstColumnInRow( nRowNo );
    while( nColNo >= 0 )
    {
      . . .
      nColNo = wndGrid.SelectionGetNextColumnInRow( nColNo, nRowNo );
    }

You can use the SelectionGetLastColumnInRow() and SelectionGetPrevColumnInRow() methods for traversing the same columns in the reversed order from right to left.

The SelectionGetForCell( nColNo, nRowNo ) method allows you to detect whether a cell is selected regardless the selection model. You can invoke the SelectionGetHitCount( nColNo, nRowNo ) method to get the number of rectangles covering the specified cell. The SelectionGetAreaCount() method returns the total number of rectangles. The SelectionGet(), SelectionInsertAt(), SelectionSetAt(), SelectionRemoveAt(), SelectionIsEmpty() and SelectionUnset() methods are handy when you need to perform any operation with the array of rectangles which describe the current selection region. The two SelectionSet() overloaded methods allows you to append a new rectangle and optionally to remove previous rectangles. The FocusSet() method sets both focus and selection at once.

You can override the OnGbwSelectionChanged() virtual method to track when selection changes. You can also override the OnGbwSelectionCanModify() virtual method and return false if the current selection region cannot be changed. You can do the same for the focus by overriding the OnGbwFocusChanged() and OnGbwFocusChanging() methods.