Arvo Bowen
Arvo Bowen

Reputation: 4929

How can I hide the current cell position on a DataGridView control?

I have a DataGridView control that allows for multi row selecting. The issue I have is with the current position of the selection being shown (see image below). I would like to NOT have the current cell position shown at all. Is that possible?

enter image description here

I would like it to look like the following instead...

enter image description here

Upvotes: 2

Views: 638

Answers (1)

OhBeWise
OhBeWise

Reputation: 5454

Derive a class from DataGridView and override the ShowFocusCues property.

  • Return True to show the focus rectangle.
  • Return False to hide the focus rectangle.
  • Return base.ShowFocusCues to maintain default behavior.

You can also expose a public property to change it dynamically.

public class DataGridViewFocused : DataGridView
{
    public bool? ShowFocus { get; set; }

    protected override bool ShowFocusCues
    {
        get
        {
            return this.ShowFocus.HasValue? this.ShowFocus.Value : base.ShowFocusCues;
        }
    }
}

Adding it to your project to replace any existing DataGridView can be as simple as navigating into your Form.Designer.cs file and replacing the following:

public System.Windows.Forms.DataGridView dataGridView1; 
this.dataGridView1 = new System.Windows.Forms.DataGridView();

with:

public DataGridViewFocused dataGridView1; 
this.dataGridView1 = new DataGridViewFocused();

From there you can always hide the focus rectangle by adding the following line:

this.dataGridView1.ShowFocus = false;

Or, for example, if you wanted to hide that rectangle only during a multiple-select event, you could do something like the following:

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (this.dataGridView1.SelectedCells.Count > 1)
    {
        this.dataGridView1.ShowFocus = false;
    }
    else
    {
        this.dataGridView1.ShowFocus = null;
    }
}

Default behavior on Tab + hiding focus rectangle on multi-select

Upvotes: 1

Related Questions