How to prevent row selection in DataGridView when clicking on the first row

Pat 0 Reputation points
2025-05-19T21:10:13.28+00:00

I have the method below that defines a DataGridView where a user will select an item which will display the details on the form. This is a C# Windows Form app using .Net Framework 4.8.1. I am using the DataGridView.CellClick event to bind the controls based on the user selection from the DataGridView. The issue is when the user clicks on the column header to sort the grid, it is selecting row 0. In the DataGridView.CellClick event handler the CurrentRow is returning 0 for the column header click as well as the first row click. How can I ignore when the user is selecting the column header?

private void ConfigureSelectionDGV()
{
    this.ManufacturerColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.ModelNumberColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.TypeColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.DescriptionColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.SelectionIDColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();

    System.Windows.Forms.DataGridViewCellStyle selectionColumnHeaderStyle = new System.Windows.Forms.DataGridViewCellStyle();
    System.Windows.Forms.DataGridViewCellStyle selectionRowStyle = new System.Windows.Forms.DataGridViewCellStyle();
    Padding newPadding = new Padding(4, 0, 0, 0);

    this.SuspendLayout();
    // establish the different styles to use
    selectionColumnHeaderStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
    selectionColumnHeaderStyle.BackColor = System.Drawing.Color.LightBlue;
    selectionColumnHeaderStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    selectionColumnHeaderStyle.ForeColor = System.Drawing.SystemColors.WindowText;
    selectionColumnHeaderStyle.SelectionBackColor = System.Drawing.SystemColors.ActiveBorder;
    selectionColumnHeaderStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
    selectionColumnHeaderStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
    
	selectionRowStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
    selectionRowStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    selectionRowStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
    selectionRowStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
    selectionRowStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

    this.selectionDataGridView.RowHeadersDefaultCellStyle = selectionColumnHeaderStyle;
    this.selectionDataGridView.DefaultCellStyle = selectionRowStyle;
    this.selectionDataGridView.BackgroundColor = this.BackColor;
    this.selectionDataGridView.DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
    this.selectionDataGridView.RowTemplate.DefaultCellStyle.Padding = newPadding;
    this.selectionDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    this.selectionDataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
    this.selectionDataGridView.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ActiveCaption;
    this.selectionDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.WindowText;
    this.selectionDataGridView.ColumnHeadersDefaultCellStyle.SelectionBackColor = this.selectionDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
    this.selectionDataGridView.EnableHeadersVisualStyles = false;
    this.selectionDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
    this.selectionDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.selectionDataGridView.RowHeadersVisible = false;
    this.selectionDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    this.selectionDataGridView.MultiSelect = false;
    //
    // Add the columns, which are defined in RecipeMaintenance_frm.Designer.cs 
    //
    this.selectionDataGridView.Columns.Clear();
    this.selectionDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                                    this.ManufacturerColumn,
                                    this.ModelNumberColumn,
                                    this.TypeColumn,
                                    this.DescriptionColumn,
                                    this.SelectionIDColumn});
    this.selectionDataGridView.AutoGenerateColumns = false;            // prevents additional columns from being added
    //
    // ManufacturerColumn
    //
    this.ManufacturerColumn.HeaderText = "Manufacturer";
    this.ManufacturerColumn.Name = "ManufacturerColumn";
    this.ManufacturerColumn.ReadOnly = true;
    this.ManufacturerColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    this.ManufacturerColumn.FillWeight = 150;
    // 
    // ModelNumberColumn
    //
    this.ModelNumberColumn.HeaderText = "Model";
    this.ModelNumberColumn.Name = "ModelNumberColumn";
    this.ModelNumberColumn.ReadOnly = true;
    this.ModelNumberColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    this.ModelNumberColumn.FillWeight = 100;
    // 
    // TypeColumn
    //
    this.TypeColumn.HeaderText = "Type";
    this.TypeColumn.Name = "TypeColumn";
    this.TypeColumn.ReadOnly = true;
    this.TypeColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    this.TypeColumn.FillWeight = 75;
    // 
    // DescriptionColumn
    //
    this.DescriptionColumn.HeaderText = "Description";
    this.DescriptionColumn.Name = "DescriptionColumn";
    this.DescriptionColumn.ReadOnly = true;
    this.DescriptionColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    this.DescriptionColumn.FillWeight = 200;

    // hide this column
    this.SelectionIDColumn.Visible = false;
    this.ResumeLayout();
    BindSelectionDGV();
}
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.
11,487 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 59,566 Reputation points
    2025-05-19T21:41:15.6+00:00

    Your event handler needs to filter out cells it doesn't care about. To filter out header/footer rows you would just look for cells that contain data.

    private void OnCellClick ( object sender, DataGridViewCellEventArgs e )
    {
       //Don't worry about non-data cells
       if (e.RowIndex == 0)
          return;
       ...
    }
    

    Alternatively, and my preferred approach, is to get the cell itself and then check the type. This makes the code more flexible if you add row and column headers. If you don't have row headers then this is less useful.

    private void OnCellClick ( object sender, DataGridViewCellEventArgs e )
    {
       var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
    
       //Don't worry about non-data cells
       if (cell is DataGridViewHeaderCell)
          return;
    
       ...
    }
    

  2. Pat 0 Reputation points
    2025-05-20T15:59:41.3+00:00

    Used Viorel's suggestion to use e.RowIndex. I thought I checked it when debugging, but must have missed it.

    if (e.RowIndex < 0) { return; }

    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.