44

How to disable Cell Highlighting in a datagridview, Highlighting should not happen even if I click on the cell.

Any thoughts please

Ramji
  • 2,536
  • 7
  • 34
  • 54

10 Answers10

107

The ForeColor/BackColor kludge wasn't working for me, because I had cells of different colors. So for anyone in the same spot, I found a solution more akin to actually disabling the ability.

Set the SelectionChanged event to call a method that runs ClearSelection

private void datagridview_SelectionChanged(object sender, EventArgs e)
{
    this.datagridview.ClearSelection();
}
Elle H
  • 11,837
  • 7
  • 39
  • 42
  • 1
    The question is [tag:vb.net] – user692942 Jun 16 '14 at 11:03
  • Tiny modifications needed for vb.net but this works well. Thank you. I don't understand why there is not a simple property to turn on or off on the control. –  May 11 '16 at 10:23
  • I would expect that "ClearSelection()" fires the "SelectionChanged" - event again. ..... but it doesn't. Strange. – marsh-wiggle Jul 09 '16 at 15:43
73

The only way I've found to "disable" highlighting is to set the SelectionBackColor and the SelectionForeColor in the DefaultCellStyle to the same as the BackColor and ForeColor, respectively. You could probably do this programmatically on the form's Load event, but I've also done it in the designer.

Something like this:

Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Me.DataGridView1.DefaultCellStyle.BackColor
Me.DataGridView1.DefaultCellStyle.SelectionForeColor = Me.DataGridView1.DefaultCellStyle.ForeColor
jheddings
  • 26,717
  • 8
  • 52
  • 65
  • 5
    This doesn't work if some cells are in different colors then default backcolor. – Jovica Oct 19 '14 at 15:50
  • 3
    If you loop through the rows, setting the colors, you can also set the selection-colors in the same loop. Then it works just fine. More generally, you can set the selection-colors any time you set the normal colors. – clweeks Dec 11 '14 at 21:12
  • 1
    I really don't like that I have to use this, but it works well :) – Jess Jul 25 '19 at 21:17
5

Did a quick websearch to find out how to make a datagridview selection non-selectable & got this (web page) hit.

Calling ClearSelection on SelectionChanged can and does cause a double firing of the SelectionChanged event, at minimum.

The first event is when the cell/row is selected and, of course, the SelectionChanged event is fired. The second firing is when ClearSelection is called as it causes (and logically so!) the selection of the datagridview to (again) changed (to no selection), thus firing SelectionChanged.

If you have more code than simply ClearSelection going on, as such I do, you'll want to suppress this event until after your code is done. Here's an example:

 private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
  //suppresss the SelectionChanged event
  this.dgvMyControl.SelectionChanged -= dgvMyControl_SelectionChanged;

  //grab the selectedIndex, if needed, for use in your custom code
  // do your custom code here

  // finally, clear the selection & resume (reenable) the SelectionChanged event 
  this.dgvMyControl.ClearSelection();
  this.dgvMyControl.SelectionChanged += dgvMyControl_SelectionChanged;
}
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
KCromm
  • 51
  • 1
  • 2
4

in vb speak:

Private Sub datagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles datagridview1.SelectionChanged
        datagridview1.ClearSelection()
End Sub
Coder22
  • 109
  • 3
3

The quickest way to do this to handle cells with different colours, without needing to refire any events, would be to do something like this:

private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
    dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.SelectionBackColor = dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.BackColor

}
You will need to put in an iterator if you allow multiple selections

(EDIT)

actually, this needs to be done at time for data population. it doesn't appear to work in the on selection changed method. So after populating the data into the table, you need to iterate through the cells and change their selected background to match their normal background. Something like this (syntax may be a little off, I'm converting it from my vb code):

foreach (datarow r in dgv.rows)
{
  foreach (datacell c in r.cells)
  {
     c.Style.SelectionBackColor = c.Style.BackColor
  }
}
LairdPleng
  • 948
  • 3
  • 9
  • 28
1

Messing around and this also works, as i only want to change the cell background colour in the 2nd column when a cell is clicked:

        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    Dim row As Integer = DataGridView1.CurrentCellAddress.Y
    Dim column As Integer = DataGridView1.CurrentCellAddress.X

    If column = 1 Then
        Me.DataGridView1.CurrentCell.Selected = False
        DataGridView1.Item(column, row).Style.BackColor = SelectColour()
    End If

End Sub
Data
  • 1,337
  • 11
  • 17
1
Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
    Me.DataGridView1.ClearSelection()
End Sub

That's it. But if you still want to get clicked row/cell index or to access values:

Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    Dim _ht As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
    If _ht.Type = DataGridViewHitTestType.Cell Then
        Me.DataGridView1.Rows(_ht.RowIndex).Cells(_ht.ColumnIndex).Value = _
        "RowIndex = " & _ht.RowIndex & ", " & "ColumnIndex = " & _ht.ColumnIndex
    End If
End Sub
LowLevel
  • 1,085
  • 1
  • 13
  • 34
1
Private Sub DGW2_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DGW2.DataBindingComplete
    Dim mygrid As DataGridView
    mygrid = CType(sender, DataGridView)
    mygrid.ClearSelection()
End Sub
DrSerhat
  • 11
  • 1
1

The answers I saw so far didn't give me exactly what I was looking for, but they showed me the right direction. In my case, after binding to a data source, the DGV selected and highlighted one cell, which I didn't want. I wanted to highlight only if the user selected the complete row.

After a while I found the following solution, which is working fine for me:

private void datagridview_SelectionChanged(object sender, EventArgs e)
{       
    var dgv = (DataGridView)sender;
    if (dgv.SelectedCells.Count == 1)
    {   // hide selection for the single cell
        dgv.DefaultCellStyle.SelectionBackColor = dgv.DefaultCellStyle.BackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.DefaultCellStyle.ForeColor;
    }
    else
    {   // show the selected cells
        dgv.DefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.SelectionBackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.SelectionForeColor;
    };
}

Note: In my example, I have set the properties

MultiSelect = false, ReadOnly = true

because I am using the DGV just to display search results.

Matt
  • 25,467
  • 18
  • 120
  • 187
0
<DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
                          HorizontalAlignment="Left" RowBackground="Transparent">