【问题标题】:datagridview auto-select columns disable c#datagridview 自动选择列禁用 c#
【发布时间】:2014-06-12 20:58:15
【问题描述】:

我有 datagridview 来显示一些数据。

我用:

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

选择整列。

当我运行应用程序 datagridview 时自动选择第一列。我正在尝试禁用它。

如果我添加:

dataGridView1.ClearSelection();

什么都没有发生。

如果我添加:

dataGridView1.Columns[-1].Selected = true;

如果在 Virtual Studio 中构建效果很好,但是在没有 Virtual Studio 的情况下构建时会出错:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index

如果我点击继续,它可以工作,但错误很烦人。

我试过了:

dataGridView1.SelectedColumns.Clear();

使用 Virtual Studio 构建时效果很好,但是在没有 Virtual Studio 的情况下构建时出现错误提示:

Collection is read-only.

这个错误也很烦人。

我认为最好的解决方案是将集合设置为非只读。但我不知道该怎么做。

任何简单的解决方案都会很棒!谢谢!

【问题讨论】:

    标签: c# collections datagridview readonly


    【解决方案1】:

    根据您的情况,您有多种可能通过 DataGridView 的自动选择:

    在表单的Load 事件上:

    dataGridView1.ClearSelection();
    

    或在表单的构造函数上(或在您初始化 DataGridView 的任何地方):

    SynchronizationContext.Current.Post(
        delegate
        {
            dataGridView1.ClearSelection();
        }, null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多