views:

2404

answers:

3

Is there a way to make a DataGridView have no cell selected? I notice even when it loses focus() it has a at least one active cell. Is there another mode that allows this? or some other trick?

+1  A: 

DataGridView.CurrentCell property can be used to clear the focus rectangle.

You can set this property (DataGridView.CurrentCell) to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx

Aleris
+2  A: 

I found that the DataGridView.CurrentCell = null didn't work for me when trying to get the requested behaviour.

What I ended up using was:

    private void dgvMyGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        if (dgvMyGrid.SelectedRows.Count > 0)
        {
            dgvMyGrid.SelectedRows[0].Selected = false;
        }

        dgvMyGrid.SelectionChanged += dgvMyGrid_SelectionChanged;
    }

It needed to be in the DataBindingComplete event handler.

Where you attach the SelectionChanged event handler doesn't affect the desired behaviour but I left it in the code snippet because I noticed for my needs at least it was better to only attach the handler after databinding, so that I avoid a selection changed event being raised for each item bound.

David Hall
+1  A: 

I spent hours to find the solution for this problem. Do this:

  1. Create a Form Project
  2. Add a DataGridView with the name "DataGridView1"
  3. Add the following code to your class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    Dim dgvRow(17) As DataGridViewRow
    Dim i As Integer
    For i = 0 To dgvRow.Length - 1
        dgvRow(i) = New DataGridViewRow()
        dgvRow(i).Height = 16
        dgvRow(i).Selected = False
        dgvRow(i).ReadOnly = True
        DataGridView1.Rows.Add(dgvRow(i))
        DataGridView1.CurrentRow.Selected = False
    Next
    End Sub
    

The importaint line of code is

    DataGridView1.CurrentRow.Selected = False

Good luck!