31

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?

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
Spooks
  • 6,937
  • 11
  • 49
  • 66

12 Answers12

38

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.

Dave Marley
  • 705
  • 6
  • 10
23

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.

DaneAnthony
  • 246
  • 2
  • 2
  • 5
    Nice 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
4

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 ...
    }
}
Jay Riggs
  • 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
2

You should call: ClearSelection after event: DataBindingComplete

user2272788
  • 373
  • 1
  • 4
  • 9
2

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

Minimihi
  • 408
  • 2
  • 11
1

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.

  1. Put dgv.ClearSelection() on DataBindingComplete Event and set GridColor to your DataGridView BackColor.

  2. Set GridColor to visible color (e.g. : Gray) on your populate method / firing event.

0

IF I understand the Q. This prevents a cell from showing selected after data binding. So the back color stays white. You can also Edit the columns and set it there.

DataGridView.DefaultCellStyle.SelectionBackColor = DataGridView.DefaultCellStyle.BackColor;
ollo
  • 24,797
  • 14
  • 106
  • 155
CW1255
  • 113
  • 1
  • 1
  • 6
0

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.

jws
  • 2,171
  • 19
  • 30
0

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;
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
0

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.

JulienD
  • 7,102
  • 9
  • 50
  • 84
-1

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();
}
Daniel
  • 20,420
  • 10
  • 92
  • 149
-1
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as 
GridView;
gv.ClearSelection();
Robert
  • 5,278
  • 43
  • 65
  • 115