如何才能禁止在VS的datagridview第一个自选? [英] How can one disable the first autoselect in a VS datagridview?

查看:141
本文介绍了如何才能禁止在VS的datagridview第一个自选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立在Visual Studio(C#),使得使用DataGridView的应用程序。现在,当我分配一个DataGridView的数据源,它会自动选择第一行,并执行我的代码以供选择。因为我经常重新分配数据源,这不是desireable。有没有办法改变它,所以它不会自动进行第一次选择,只有依赖于用户的选择?

I have created an application in Visual Studio (C#) that makes use of a datagridview. Now, when I assign the DataSource of that datagridview, it automatically selects the first row, and executes my code for selection. Since I frequently reassign that datasource, this is not desireable. Is there any way to change it so it doesn't automatically make that first select, and only relies on the user's selections?

谢谢!

在回应的Darshan乔希的评论:
除了自动生成的代码,改变对datagridview的唯一的事情就是设置的AutoGenerateColumns为false,并设定DataSource属性。
我已经放在一个MessageBox.Show在我的SelectionChanged委托,似乎它甚至被调用数据源设置三次每次。一旦之前的数据加载,并经过两次。

In response to the comment of Darshan Joshi: Apart from the auto-generated code, the only thing altered on the datagridview is setting AutoGenerateColumns to false, and setting the DataSource property. I've placed a MessageBox.Show in my selectionchanged delegate, and it seems it even gets called thrice every time the datasource is set. Once just before the data is loaded, and twice after.

我无法设置选择假负载,因为数据源是特定用户操作后设置,而不是初始化。

I can't set selected to false on load, since the datasource is set after certain user actions, not on initialization.

推荐答案

我有同样的问题,这里是我的解决方案。

I had the same problem and here is my solution.

最棘手的部分是找到在哪里清除选择......我们以后可以选择已在DataGridView仅设置清除选择。起初选择只准备在Form.Load事件被清除,但DataGridView.DataSource的subsiquent设置选择准备将DataSource转让后,被直接清除。

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.

这篇关于如何才能禁止在VS的datagridview第一个自选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆