One funny thing about the CurrentCell property is that, while the GridView provides a lot of events, there is currently (as of builds 1480 for Flash & 1551 for Silverlight) no OnCurrentCellChanged event that you can handle to be informed of a change of the active cell.
Well, in fact this is not really funny, especially if you need to be informed of a CurrentCell change... So, how can we workaround?
My first tentative was to use the OnRowStateChanged and OnColumnStateChanged events, but this prove to be not reliable as the behavior of these events depend of the grid SelectionMode. By example, if using FullRowSelect selection mode, the OnColumnStateChanged is never triggered.
After a lot of thinking, I came to the conclusion that if the CurrentCell changes, there is one thing that is very likely to happen: a repaint of the grid. Therefore I tried overriding OnPaint, and it seems to work quite well:
public class MyGridView : GridView
{
private Point previousCurrentCellAddress = Point.Empty;
public event EventHandler CurrentCellChanged;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Point currentCellAddress = CurrentCellAddress;
if (currentCellAddress != previousCurrentCellAddress)
{
previousCurrentCellAddress = currentCellAddress;
OnCurrentCellChanged(EventArgs.Empty);
}
}
protected virtual void OnCurrentCellChanged(EventArgs e)
{
if (CurrentCellChanged != null)
CurrentCellChanged(this, e);
}
}
Hope this helps,
Fred
No comments:
Post a Comment