Spooks
Spooks

Reputation: 7177

DataGridView first column,first row, is selected on Load, I don't want this

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?

Upvotes: 31

Views: 58484

Answers (12)

user11000492
user11000492

Reputation: 1

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

Upvotes: -1

gireesh mohan
gireesh mohan

Reputation: 1

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.

Upvotes: 0

Robert Timothy
Robert Timothy

Reputation: 86

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.

Upvotes: 2

Dennis Collins
Dennis Collins

Reputation: 11

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;

Upvotes: 0

Lusine Mkrtchyan
Lusine Mkrtchyan

Reputation: 59

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();
}

Upvotes: -1

Dave Marley
Dave Marley

Reputation: 715

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.

Upvotes: 39

jws
jws

Reputation: 2764

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.

Upvotes: 0

user2272788
user2272788

Reputation: 373

You should call: ClearSelection after event: DataBindingComplete

Upvotes: 2

CW1255
CW1255

Reputation: 121

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;

Upvotes: 0

DaneAnthony
DaneAnthony

Reputation: 246

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.

Upvotes: 23

Minimihi
Minimihi

Reputation: 418

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

Upvotes: 3

Jay Riggs
Jay Riggs

Reputation: 53595

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

Upvotes: 5

Related Questions