In one of my projects, I had to load around 200000 rows in the CGridCtrl and then had to provide the functionalitiy of EnsureVisible to scroll to and highlight a row. So I had to come up with a reasonably fast algorithm.
void GVEnsureVisible(CGridCtrl* pGridCtrl, int nRow)
int nItems = pGridCtrl->GetItemCount();
int nPos = nRow;
if(nItems > 0)
double dx = (double)si.nMax / (double)nItems;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = (int)(dx * nPos) - si.nPage/2;
pGridCtrl->SetScrollInfo(SB_VERT, &si, TRUE);
Points of Interest
This code will only work if all the rows of the Grid are of same size.
Article Uploaded : 5th March, 2012.