C # WinForms DataGridView - Constant Row Selected!

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

The following are the settings for Datagridview:

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"; 
+6
c # winforms datagridview
source share
4 answers

You should use DataGridView.ClearSelection () to remove any selections after you populate your DataGridView.

You can also allow only certain columns that restrict editing only in the checkbox column. See DataGridViewColumn.ReadOnly Property

+3
source share

Goober, I ran into a similar problem when I needed a user to select rows using checkboxes. The first row is always selected by default after the gridview is full regardless of the gridview settings. To make sure that the first row is not selected, each time the gridview fills up, do ClearSelection ():

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

ClearSelection () clears all selected rows.

+4
source share

Make sure your does NOT call a method to load data from the form constructor. If you call it from .load () form

also after loading datagridview do this

DataGridView.Rows [0] .Selected = false;

0
source share

Select datagridview. Then in the properties window, scroll down until you find the SelectionMode property and change it to FullColumnSelect.

Alternatively, if you want them to just select one checkbox at a time, change it to CellSelect

-one
source share

All Articles