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?
13 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.
5 Comments
WPF!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.
2 Comments
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 ... } } 1 Comment
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!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 Comment
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.
Comments
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.
Comments
I found that the TabStop = false setting and ClearSelection() call should happen after the DataGridView is shown to reach the desired effect. If you use Sort() it should happen also before the steps above. Otherwise the cleared selection comes back after the control gets shown.
What was working for me:
// subscribe to your window's Shown event which guarantees Controls are shown private void form_Shown(object sender, EventArgs e) { this.myDataGridView.DataSource = myDataSource; // optional, but manipulates the selection // so must be done before the next steps this.myDataGridView.Sort(myColumn, ListSortDirection.Descending); // these mentioned in other answers, // may be placed in DataBindingComplete handler too // including the Sort() above! this.myDataGridView.TabStop = false; this.myDataGridView.ClearSelection(); // to avoid the initial unwanted SelectionChanged // subscribe postponed until here this.myDataGridView.SelectionChanged += selectionChanged; } Comments
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e) GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as GridView; gv.ClearSelection();