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();
}