3

I have a datagridview which only purpose is to display information from the database. I have also added extra column which has "View" link in every row, for some reason. So basically the datagridView's purpose is just for the user to click "View" link. That's all. That's why I don't want the "highlight" thingy.

I've been searching for this answer but I still haven't found the right answer. Is it possible or not?

Syntax Error
  • 105
  • 1
  • 4
  • 12

4 Answers4

8

This is how you do this:

dataGridView1.Rows.Add("a1");//Just for testing
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black; //This is the text color 

You can choose any other color if that's your default. But just set the SelectionBackColor to the background color of your datagrid.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
5

The simplest way I've found:

dataGridViewCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle.SelectionForeColor = System.Drawing.Color.Transparent;
Todd Hill
  • 51
  • 1
  • 3
0

Easiest way to remove the display of a selection is to set the selection colour to match the cell background colour:

dataGridView1.DefaultCellStyle.SelectionBackColor
  = dataGridView1.DefaultCellStyle.BackColor;

Referencing the existing background colour means you don't have a hard-coded colour.

aforest-ccc
  • 85
  • 1
  • 11
0

You might try the following solution so that you do not have to set the selection background color on each new screen.

  1. Create a custom DataGridView to be used by your application.
  2. In the constructor, you can handle the DataGridView's ColumnStateChanged event and reset the HeaderCell's Style to the default background value:

e.g:

public class DataGridViewCustom : DataGridView
  {
    public DataGridViewSearch() : base()
    {
      InitializeComponent();
  
      this.ColumnStateChanged += DataGridViewCustom_ColumnStateChanged;
    }
  
    private void DataGridViewCustom_ColumnStateChanged(object sender, DataGridViewColumnStateChangedEventArgs e)
    {
      DataGridView dataGridView = (DataGridView)sender;
      //Only update for full row selection mode.
      if (dataGridView.SelectionMode == DataGridViewSelectionMode.FullRowSelect)
      {
        e.Column.HeaderCell.Style.SelectionBackColor = dataGridView.ColumnHeadersDefaultCellStyle.BackColor;
      }
    }
  }
Darrel K.
  • 1,611
  • 18
  • 28