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);
RSS Feed