The DataGridView is a powerful grid control included in the .NET Framework.  One function missing, however, is the ability to hide the current selection when the DataGridView control is not focused.  What the DataGridView class needs is a HideSelection property, similar to the ListView and TextBox.  But the .NET designers have not included this capability in the DataGridView class.

Following is a control derived from DataGridView that will automatically hide the current selection when a DataGridView loses focus, and show the selection when the DataGridView regains focus:

public class DataGridViewEx : DataGridView
{
    private DataGridViewCell m_Cell;
    protected override void OnGotFocus( EventArgs e )
    {
        if (this.m_Cell != null)
            this.CurrentCell = this.m_Cell;
        base.OnGotFocus( e );
    }
    protected override void OnLostFocus( EventArgs e )
    {
        this.m_Cell = this.CurrentCell;
        this.CurrentCell = null;
        base.OnLostFocus( e );
    }
}
Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot