DataGridView: Unable to 'De-select' Cell

Tony Hedge 6 Reputation points
2024-03-19T16:21:10.08+00:00

I am having trouble removing the blue background from a DataGridView Cell (2 actually).

The DataGridViews have NO selected cells and NO CurrentCells (CurrentCellAddress = -1, -1),

Neither DataGridView is the ActiveControl, yet the Blue Background persists: one Cell in each.

See screenshot below in which the 'End' button is the active control.What does the blue background mean? How do I get rid of it? Both DGViews are ReadOnly.DGV Selected Cells

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,918 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,614 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 117.3K Reputation points
    2024-03-19T17:50:17.4466667+00:00

    Try to edit the DefaultCellStyle property of the grids (in Form Designer). Change the SelectionBackColor. Use a green colour that is used for your columns.

    0 comments No comments

  2. Jiale Xue - MSFT 45,006 Reputation points Microsoft Vendor
    2024-03-20T08:19:53.13+00:00

    Hi @Tony Hedge , Welcome to Microsoft Q&A,

    You can clear selected cells using ClearSelection(). At the same time, modify the enable attribute so that it cannot be selected.

    using System.Windows.Forms;
    
    namespace xxx
    {
         public partial class Form1 : Form
         {
             public Form1()
             {
                 InitializeComponent();
    
                 // Set up the first DataGridView
                 dataGridView1.ReadOnly = true;
                 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                 dataGridView1.AllowUserToAddRows = false; // Disable users from manually adding rows
    
                 // Set up the second DataGridView
                 dataGridView2.ReadOnly = true;
                 dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                 dataGridView2.AllowUserToAddRows = false; // Disable users from manually adding rows
    
    
                 //Add sample data for two DataGridViews
                 // This assumes you already have a data source, otherwise you need to set the data source for the two DataGridViews
                 // Just added some sample data here for demonstration purposes
                 dataGridView1.Columns.Add("Column1", "Column1");
                 dataGridView1.Columns.Add("Column2", "Column2");
                 dataGridView1.Rows.Add(new object[] { "Data1", "Data2" });
                 dataGridView1.Rows.Add(new object[] { "Data3", "Data4" });
    
                 dataGridView2.Columns.Add("Column1", "Column1");
                 dataGridView2.Columns.Add("Column2", "Column2");
                 dataGridView2.Rows.Add(new object[] { "Data5", "Data6" });
                 dataGridView2.Rows.Add(new object[] { "Data7", "Data8" });
    
                 dataGridView1.Enabled = false; // Disable the first DataGridView
                 dataGridView2.Enabled = false;
             }
    
             private void Form1_Load(object sender, System.EventArgs e)
             {
                 dataGridView1.ClearSelection();
                 dataGridView2.ClearSelection();
             }
         }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.