Skip to main content Skip to footer

Scrolling into view with WinForms and WPF FlexGrids

When end users need the ability to locate a particular row in a grid, and automatically scroll the grid so that row is at the top or bottom of the viewable area, the ScrollIntoView method is a common way to accomplish this. The following samples (one for WinForms, and another for WPF), have a textbox where users can enter the row they wish to scroll to, and a button to execute the scroll. They also have a dropdown so that users can select a select particular option, and execute the scroll upon selection. Here is an example of the code used:


<div id="_mcePaste">private void ScrollToView(string text)</div>  
<div id="_mcePaste">{</div>  
<div id="_mcePaste">try</div>  
<div id="_mcePaste">{</div>  
<div id="_mcePaste">int rowInFocus = Int32.Parse(text);</div>  
<div id="_mcePaste">int fixedRow = c1FlexGrid1.Rows.Frozen+1;</div>  
<div id="_mcePaste">double offset = c1FlexGrid1.Rows[rowInFocus].Top - c1FlexGrid1.Rows[fixedRow].Top;</div>  
<div id="_mcePaste">c1FlexGrid1.ScrollPosition = new Point(0, -offset);</div>  
<div id="_mcePaste">}</div>  
<div id="_mcePaste">catch (Exception e)</div>  
<div id="_mcePaste">{</div>  
<div id="_mcePaste">MessageBox.Show(e.ToString());</div>  
<div id="_mcePaste">}</div>  
<div></div>  
<div>

Here is a screenshot of the sample in action:

Here are the links to download the samples:

WinForms Sample

WPF Sample

MESCIUS inc.

comments powered by Disqus