【问题标题】:C# WinForms DataGridView - Constant Row Selected!C# WinForms DataGridView - 选择常量行!
【发布时间】:2009-07-06 10:35:56
【问题描述】:

我有一个winforms datagridview,它似乎始终至少选择了一行。我对能够选择行完全不感兴趣,我只需要用户能够选择第 1 列中的复选框。 任何想法为什么总是至少选择 1 行? 我怎样才能防止这种情况? 会不会影响column1的复选框的选择?

以下是我的 Datagridview 设置:

this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
this.dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.WhiteSmoke;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
this.dataGridView1.ColumnCount = 0;

colSelect = new DataGridViewCheckBoxColumn();
colSelect.HeaderText = "Select Message";
colSelect.Width = 90;
this.dataGridView1.Columns.Insert(0, colSelect);
this.dataGridView1.Columns[0].DataPropertyName = "msgSelect";

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    Goober,我遇到了类似的问题,我需要用户使用复选框来选择行。在填充 gridview 后,无论 gridview 设置如何,默认情况下始终选择第一行。为确保第一行未被选中,每次填充 gridview 时,执行 ClearSelection():

    this.dgridvw.DataSource = this.MyTable;
    this.dgridvw.ClearSelection();
    

    ClearSelection() 清除所有选定的行。

    【讨论】:

      【解决方案2】:

      在填充 DataGridView 后,您应该使用 DataGridView.ClearSelection() 删除任何选择。

      您还可以将特定列设置为只读允许,这将允许将编辑限制为您的复选框列。见DataGridViewColumn.ReadOnly Property

      【讨论】:

        【解决方案3】:

        确保您调用从表单构造函数加载数据的方法。如果你从 form.load() 调用它

        在加载数据网格视图后也这样做

        DataGridView.Rows[0].Selected = false;

        【讨论】:

          【解决方案4】:

          选择数据网格视图。然后,在属性窗口中向下滚动,直到找到属性 SelectionMode 并将其更改为 FullColumnSelect。

          或者,如果您希望他们一次只选择一个复选框,请将其更改为 CellSelect

          【讨论】:

          • 真的吗?之所以选择一行,是因为这是您的选择模式设置的!因此,要停止选择“一行”,您需要更改它...
          • 我认为你没有理解 Calanus 的问题。他想知道如何阻止 DataGridView 默认选择列表中的第一行。
          猜你喜欢
          • 2011-12-30
          • 2018-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-09
          • 1970-01-01
          • 2015-07-12
          相关资源
          最近更新 更多