How to disable first automatic selection in VS datagridview?

I created an application in Visual Studio (C #) that uses datagridview. Now, when I assign a DataSource to this datagridview, it automatically selects the first row and runs my code to select. Since I often reassign this data source, this is undesirable. Is there a way to change it so that it does not automatically make this first choice and is based only on user settings?

Thanks!

In response to Darshan Joshi's comment: Besides the automatically generated code, the only thing that has changed in the datagridview is to set the AutoGenerateColumns value to false and set the DataSource property. I put MessageBox.Show in my delegate with the selection selected, and it seems that it even gets thrice every time the data source is installed. Once before downloading data and twice after.

I cannot set the value to false at boot time, since the data source is installed after certain user actions, and not during initialization.

+6
source share
8 answers

I had the same problem and here is my solution.

The hard part was finding where you can clear the selection ... We can only clear the selection after the selection has been set by DataGridView. At first, the selection is only ready for cleaning in the Form.Load event, but the subsiquent settings for the DataGridView.DataSource selection is ready for cleaning immediately after the DataSource is assigned.

public class DataGridView_AutoSelectSuppressed : DataGridView { private bool SuppressAutoSelection { get; set; } public DataGridView_AutoSelectSuppressed() : base() { SuppressAutoSelection = true; } public new /*shadowing*/ object DataSource { get { return base.DataSource; } set { SuppressAutoSelection = true; Form parent = this.FindForm(); // Either the selection gets cleared on form load.... parent.Load -= parent_Load; parent.Load += parent_Load; base.DataSource = value; // ...or it gets cleared straight after the DataSource is set ClearSelectionAndResetSuppression(); } } protected override void OnSelectionChanged(EventArgs e) { if (SuppressAutoSelection) return; base.OnSelectionChanged(e); } private void ClearSelectionAndResetSuppression() { if (this.SelectedRows.Count > 0 || this.SelectedCells.Count > 0) { this.ClearSelection(); SuppressAutoSelection = false; } } private void parent_Load(object sender, EventArgs e) { ClearSelectionAndResetSuppression(); } } 

Hope this helps.

+1
source

You must call: ClearSelection after the event: DataBindingComplete

+5
source

you can cancel it in your form_load event as

  private void Form1_Load(object sender, EventArgs e) { dataGridView1.Rows[0].Selected = false; } 
0
source

To load a grid without any selection, you can use this piece of code.

  GridView.CurrentCell = null; 

This will load it simply, without any choice. Add this after assigning the data source to the grid.

0
source

Make sure your does NOT call a method to load data from the form constructor. If you call it from Form.load ()

also after loading myDataGridView do this

myDataGridView.Rows [0] .Selected = false;

0
source

This worked for me:

Cancel the SelectionChanged event before binding the data and then re-register the event. Perhaps you should remove the event registration in the designer and register the event manually in your code.

myDGV.SelectionChanged -= new System.EventHandler(this.myDGV_SelectionChanged);

0
source

Your experience may differ, but in my work I often struggle with this when the grid loads for the first time, and although it was found that many solutions are acceptable (and much more simply ridiculous), they do not all work in every scenario. The only solution that I think is most suitable (for me, again, your experience and scenarios may differ) is processing DataGridView.VisibleChanged:

  public ThingWithGrid() { Grid.VisibleChanged += Grid_VisibleChanged; } private void Grid_VisibleChanged(object sender, EventArgs e) { UpdateSelectedRows(InitialSelection); Grid.VisibleChanged -= Grid_VisibleChanged; } 
0
source

Just add SelectedItem = "- 1", this will do the trick

 <DataGrid Name="dataGrid" SelectedItem="-1" /> 
-1
source

Source: https://habr.com/ru/post/925763/


All Articles