The row/column indices in the grid data provider differ from those in the grid window but it is not difficult to establish a conversion between them. Here is how it can be done with row indices. In the grid window, row numbers are zero-based and all rows in the data area, in the header at top and in the header at bottom are enumerated independently:
Header row 0 at top
Header row 1 at top
Header row 2 at top
Data row 0
Data row 1
Data row 2
Data row 3
Data row 4
Data row 5
Header row 0 at bottom
Header row 1 at bottom
Header row 2 at bottom
You can get any row count using the
CExtGridWnd
’s API:
LONG nOuterRowCountAtTheTop = wndGrid.OuterRowCountTopGet();
LONG nInnerDataRowCount = wndGrid.RowCountGet();
LONG nOuterRowCountAtTheBottom = wndGrid.OuterRowCountBottomGet();
In the data provider, data rows are stored in a two dimensional memory data structure, which has the following layout:
Real index 0 --> Header row 0 at top
Real index 1 --> Header row 1 at top
Real index 2 --> Header row 2 at top
Real index 3 --> Header row 0 at bottom
Real index 4 --> Header row 1 at bottom
Real index 5 --> Header row 2 at bottom
Real index 6 --> Data row 0
Real index 7 --> Data row 1
Real index 8 --> Data row 2
Real index 9 --> Data row 3
Real index 10 --> Data row 4
Real index 11 --> Data row 5
During row sorting only the grid’s data rows get swapped to reach the desired sort order. The data provider notifications use data provider’s internal row indices described above. So, if you want to convert row numbers passed in the
nRowColNo1
and
nRowColNo2
parameters of the
OnDataProviderSwapSeries()
virtual method, then you should subtract the summary number of header rows:
LONG nOuterRowCountAtTheTop = wndGrid.OuterRowCountTopGet();
LONG nOuterRowCountAtTheBottom = wndGrid.OuterRowCountBottomGet();
LONG nTotalCountOfHeaderRows = nOuterRowCountAtTheTop + nOuterRowCountAtTheBottom;
LONG nZeroBasedDataRowIndexInGrid1 = nRowColNo1 - nTotalCountOfHeaderRows;
LONG nZeroBasedDataRowIndexInGrid2 = nRowColNo2 - nTotalCountOfHeaderRows;
#if (defined _DEBUG)
LONG nInnerDataRowCount = wndGrid.RowCountGet();
ASSERT( 0 <= nZeroBasedDataRowIndexInGrid1 && nZeroBasedDataRowIndexInGrid1 < nInnerDataRowCount );
ASSERT( 0 <= nZeroBasedDataRowIndexInGrid2 && nZeroBasedDataRowIndexInGrid2 < nInnerDataRowCount );
#endif (defined _DEBUG)
You should use the
nZeroBasedDataRowIndexInGrid1
and
nZeroBasedDataRowIndexInGrid2
variables in your row position tracking code instead of the
nRowColNo1
and
nRowColNo2
variables.
The same technique can be used for column indices.