…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);

Advertisements

Lisa kommentaar »

Kommentaare veel pole.

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Täida nõutavad väljad või kliki ikoonile, et sisse logida:

WordPress.com Logo

Sa kommenteerid kasutades oma WordPress.com kontot. Logi välja /  Muuda )

Google+ photo

Sa kommenteerid kasutades oma Google+ kontot. Logi välja /  Muuda )

Twitter picture

Sa kommenteerid kasutades oma Twitter kontot. Logi välja /  Muuda )

Facebook photo

Sa kommenteerid kasutades oma Facebook kontot. Logi välja /  Muuda )

w

Connecting to %s

Create a free website or blog at WordPress.com.