Click here to Skip to main content
Click here to Skip to main content

Tagged as

Fast Ensure Visible for CGridCtrl

, 5 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip shows a very fast algorithm (with some constraints enforced for it to work) to EnsureVisible a row in CGridCtrl by Chris Maunder



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. 


The code

// This function will only work if all the grid control rows are same in size

void GVEnsureVisible(CGridCtrl* pGridCtrl, int nRow)
	if(pGridCtrl== NULL)
	int nItems = pGridCtrl->GetItemCount();
	int nPos = nRow;
	if(nItems > 0)
		pGridCtrl->GetScrollInfo(SB_VERT, &si);
		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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.
Follow on   Twitter

Comments and Discussions

QuestionCan you explain how this makes fast scroll? Also compare it with EnsureVisible, too. Pinmemberehaerim24-Oct-13 10:56 
AnswerRe: Can you explain how this makes fast scroll? Also compare it with EnsureVisible, too. PinmemberMukit, Ataul25-Oct-13 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 5 Mar 2012
Article Copyright 2012 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid