How can one disable the first autoselect in a VS datagridview?

半腔热情 提交于 2019-12-03 14:14:18

I had the same problem and here is my solution.

The tricky part was finding where to clear the selection... We can only clear the selection after the selection has been set by the DataGridView. At first the selection is only ready to be cleared in the Form.Load event, but subsiquent settings of the DataGridView.DataSource the selection is ready to be cleared straight after the DataSource assignment.

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.

You should call: ClearSelection after event: DataBindingComplete

you can deselect it in your form_load event like

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

To load the grid without any selection, you can use this code snippet.

 GridView.CurrentCell = null;

This will load it plain, without any selection. Add this after assigning data-source to the grid.

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 myDataGridView is loaded do this

myDataGridView.Rows[0].Selected = false;

This worked for me:

Deregister the SelectionChanged event just before binding the data and then re-register the event. Maybe you should delete the registration of the event in the designer and register the event manually in your code.

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

Your experience may vary, but in my work I frequently fight this when the grid first loads, and although many solutions have been found to be acceptable (and far more are just ludicrous), they don't all work in every scenario. The one solution I find that works the most (for me, again your experience and scenarios may vary) is handling DataGridView.VisibleChanged:

    public ThingWithGrid() {
        Grid.VisibleChanged += Grid_VisibleChanged;
    }

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

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

<DataGrid Name="dataGrid" SelectedItem="-1" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!