27
votes

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?

12
why is having the first row,col selected a problem?Beth
The marketing people said so... :(Spooks

12 Answers

33
votes

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.

20
votes

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.

4
votes

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 ...
    }
}
2
votes

You should call: ClearSelection after event: DataBindingComplete

1
votes

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

1
votes

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
votes

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;
0
votes

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.

0
votes

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;
0
votes

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.

-1
votes

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();
}
-1
votes
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as 
GridView;
gv.ClearSelection();