…meie igapäevast IT’d anna meile igapäev…

2009-03-13

How to scroll DataGrid programmatically in .NET CE?

It is easy to scroll the DataGrid in full .NET Framework:

GridVScrolled(this, new ScrollEventArgs(ScrollEventType.LargeIncrement, rowNum));

– or even easier, just use DataGridView and DataGridView.FirstDisplayedScrollingRowIndex() property.

However, in .NET Compact Edition there is no DataGridView – and no GridVScrolled. So there is no easy method to scroll a certain row to the view – but that is exactly what I needed: if the DataGrid is resized, selected row must remain to be visible.

So I ended up using System.Reflection to scroll my grid – or to be more exact, I derived my own class from DataGrid and added a method ScrollToRow():

using System.Reflection;
...
class BaseGrid : DataGrid {
 
/// <summary>
/// Scrolls datagrid to the row.
/// </summary>
/// <param name="rowNum">The row num.</param>
public void ScrollToRow(int rowNum)
{
  FieldInfo fi = this.GetType().GetField("m_sbVert", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
  ((VScrollBar)fi.GetValue(this)).Value = rowNum;
}
 
}

And then simply call baseGrid.ScrolToRow(baseGrid.CurrentRowIndex);

Lisa kommentaar »

Kommentaare veel pole.

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Muuda )

Twitter picture

You are commenting using your Twitter account. Log Out / Muuda )

Facebook photo

You are commenting using your Facebook account. Log Out / Muuda )

Google+ photo

You are commenting using your Google+ account. Log Out / Muuda )

Connecting to %s

Blog at WordPress.com.