So basically the very first column in the first row is always selected, I can't figure out a way to have it so the gridview has no selected cells. Any help?
-
why is having the first row,col selected a problem? – Beth Oct 08 '10 at 15:35
-
16The marketing people said so... :( – Spooks Oct 08 '10 at 15:36
12 Answers
I was having quite a bit of trouble with this myself. I have a user control with a DataGridView that is populated on application/form load. The selection of the first row seems to happen after databinding is complete and the grid is populated/rendered. The user control load event (and presumably, form load as well) fires prior to that - so calling gridView.ClearSelection() or nullifying gridView.CurrentCell in those load events has no net effect.
What finally worked for me was calling .ClearSelection() from the DataBindingComplete event of the DataGridView itself. This worked like a charm.

- 705
- 6
- 10
-
1This worked for me, but the grid's SelectionChanged event still gets called after this with the row selected, so I have to have ugly flag logic throughout my form to figure out it the user selected a row or not. – Jun 14 '12 at 23:23
-
I tried the ticked answer and that didn't work for me, this answer works! Thanks Dave. – Harag Jun 10 '15 at 10:15
-
2
-
1
-
1
I had this same issue and nothing was working. The solution that worked for me was setting the 'Tabstop' property to False and calling the ClearSelection() method immediately after the data bind.

- 246
- 2
- 2
-
5Nice tip. This still fires the SelectionChanged event though, which could cause problems for some. – Bjørn Otto Vasbotten Mar 01 '13 at 13:11
-
1@Above You can unbind the selection changes event by doing something like this. " MyDataGrid.SelectionChanged -= this.MyDataGrid_SelectionChanged;" //do some operations then rebind the selection change event by doing this, " MyDataGrid.SelectionChanged += this.MyDataGrid_SelectionChanged;" i'm not sure if this causes some performance problems. – Hari Jul 23 '14 at 09:28
Set the DGV's CurrentCell
property to null after data binding the DGV:
dataGridView1.CurrentCell = null;
Note that doing this won't prevent DGV events associated with row and cell selection from firing; you'll have to add selected row or cell count checks on RowEnter events, something like this:
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.SelectedRows.Count == 1) {
// Do stuff since a row is actually selected ...
}
}

- 53,046
- 9
- 139
- 151
-
Setting the equivalent in vb.net (`CurrentCell = Nothing`) solved the issue I came to this question for. Basically, even after running `.ClearSelection()` on the DataGridView (with SelectionMode being FullRowSelect), there was still a little cursor indicator on the first row despite it not being highlighted. And it was preventing the `RowEnter` event from firing if you clicked the first row prior to clicking a different row. Thanks! – Chad Jul 17 '19 at 15:08
after bounding data just call
dataGridView.ClearSelection();
I think you tried to call it before setting data to dataGrindView, if you even ever tried it

- 408
- 2
- 11
-
You should calls this at DataBindingComplete event of data grid view – Mȍhǟmmǟd Șamȋm Jan 21 '20 at 11:12
I had the same issue in my case, instead of set the first row visibility to false. It would be better to set the GridColor value to avoid risk on SelectionChanged Event.
Put dgv.ClearSelection() on DataBindingComplete Event and set GridColor to your DataGridView BackColor.
Set GridColor to visible color (e.g. : Gray) on your populate method / firing event.

- 76
- 12
I also wanted read-only DataGridView, and in my case, a separate thread is slowly obtaining data and handing it to the GUI thread via a multi-thread list, and Form timer. In this approach, the GUI thread expands the data grid as needed while allowing browse.
With suggestions above the selection could be hidden, but none could prevent the cell from getting reset when my GUI thread calls dataGridView.Rows.Add() with a selection. This includes hooking events to prevent the selection, and disabling edit mode.
I found the behavior I wanted with
dataGridView.AllowUserToAddRows = false;
Now I have a dynamically sized, asynchronously loaded data grid that is read-only.
I did not like the BackgroundWorker solution, because progress is quite a burden on my loading code. Nor did I like the requirement to rebuild a new DataTable every refresh of the grid. I could not find any hints on refreshing the DataGridView with one DataTable that is being built up, but it seems like this should be possible.

- 2,171
- 19
- 30
Make sure your are not calling the method to load the data from the form constructor. If you call it from the form.load()
also after the datagridview is loaded do this
DataGridView.Rows[0].Selected = false;

- 6,792
- 8
- 50
- 57

- 11
- 4
Most of the time, it is caused by a small mistake, maybe the datagridview
is set on a group box. If there are more group boxes then the selection will stop on the first group box, so keep the group box by priority basis.

- 7,102
- 9
- 50
- 84
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
this.ClearSelection();
}

- 20,420
- 10
- 92
- 149

- 59
- 1
- 5
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as
GridView;
gv.ClearSelection();

- 5,278
- 43
- 65
- 115