【问题标题】:Dynamically Added CheckBox Column is Disabled in GridViewGridView 中禁用了动态添加的复选框列
【发布时间】:2010-10-24 07:22:10
【问题描述】:

我正在动态地将布尔列添加到数据集。 DataSet 的表是 GridView 的 DataSource,它自动生成列。

问题:此动态生成的列的复选框全部被禁用。如何启用它们?

ds.Tables["Transactions"].Columns.Add("Retry", typeof(System.Boolean));
ds.Tables["Transactions"].Columns["Retry"].ReadOnly = false;

换句话说,如何控制 GridView 为布尔字段生成 CheckBoxes 的方式? (以及为什么将 ReadOnly 设置为 False 没有效果?)

谢谢!

【问题讨论】:

    标签: c# dynamic dataset checkbox


    【解决方案1】:

    您可能应该添加 CellContentClick 事件处理程序并否定单元格已有的值。喜欢:

    private void dgvRaceDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
                if (e.ColumnIndex == 5)
                {
                    dgvRaceDetails.Rows[e.RowIndex].Cells[5].Value =
                        !(Boolean)dgvRaceDetails.Rows[e.RowIndex].Cells[5].Value;
                }
    
                if (e.ColumnIndex == 6)
                {
                    dgvRaceDetails.Rows[e.RowIndex].Cells[6].Value =
                        !(Boolean)dgvRaceDetails.Rows[e.RowIndex].Cells[6].Value;
                }
    }
    

    【讨论】:

      【解决方案2】:

      我相信它们会被自动禁用,直到它们有值。

      DataRow row = ds.Tables["Transactions"].NewRow();
      row("Retry") = true;
      ds.Tables["Transactions"].Rows.Add(row)
      

      【讨论】:

      • 没有区别。现在它们已被检查但仍被禁用。 foreach (DataRow dr in ds.Tables["Transactions"].Rows) { dr.BeginEdit();博士 [“重试”] = true;博士结束编辑(); }
      • 你是对的。肯定有其他代码影响了您的专栏。
      【解决方案3】:

      将布尔值设置为真。它的默认值为假。我希望这会有所帮助。

      【讨论】:

      • 将ReadOnly 设置为true 如何使它们不被禁用?
      • 我读错了。添加 true/false 作为值将使它们选中/取消选中并且未启用/禁用。在我的代码中,我试图禁用复选框,而我的复选框不会这样做。
      • dataTable.Columns.Add("ColumnName", typeof(bool));
      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 2012-07-25
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多