C# WinForms DataGridView - Constant Row Selected!

12,293

Solution 1

You should use DataGridView.ClearSelection() to remove any selection(s) after you have populated your DataGridView.

Also you can make specific columns read only allow which would allow to restrict editing to your checkbox column only. See DataGridViewColumn.ReadOnly Property

Solution 2

Goober, I encountered a similar problem, where I needed user to select rows using checkboxes. The first row is always selected by default after the gridview is populated irrespective of the gridview settings. To make sure the first row is not selected, everytime the gridview is populated, do a ClearSelection():

this.dgridvw.DataSource = this.MyTable;
this.dgridvw.ClearSelection();

ClearSelection() clears all the selected rows.

Share:
12,293
cheunology
Author by

cheunology

Updated on June 19, 2022

Comments

  • cheunology
    cheunology almost 2 years

    I have a winforms datagridview that seems to always have at least one row selected all the time. I'm not interested in being able to select the rows at all really, I just need the user to be able to select the checkbox in column 1. Any ideas why there is always at least 1 row selected? How can i prevent this? Will it affect the ability to select the checkbox in column1?

    Below are my Datagridview settings:

    this.dataGridView1.AllowUserToAddRows = false;
    this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    this.dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
    this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    this.dataGridView1.MultiSelect = false;
    this.dataGridView1.RowHeadersVisible = false;
    this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.WhiteSmoke;
    this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
    this.dataGridView1.ColumnCount = 0;
    
    colSelect = new DataGridViewCheckBoxColumn();
    colSelect.HeaderText = "Select Message";
    colSelect.Width = 90;
    this.dataGridView1.Columns.Insert(0, colSelect);
    this.dataGridView1.Columns[0].DataPropertyName = "msgSelect";