【问题标题】:Dynamically written DataGridView columns ReadOnly Property is not working动态写入的 DataGridView 列 ReadOnly 属性不起作用
【发布时间】:2013-02-26 08:41:14
【问题描述】:

我在表单加载中将 ReadOnly 属性设置为 true

dgvDcNon.Columns["itemDiscrip"].ReadOnly = true;

但该属性不起作用。我应该采取更多步骤来实现这一点吗?

我在editcontrolshowing 事件中将自动完成模式设置为datagridview。有什么原因吗? 我的代码是

           if (e.Control is TextBox)
            {
                TextBox tbValid = e.Control as TextBox;
                tbValid.KeyPress += new KeyPressEventHandler(tbValid_KeyPress);
            }
            String[] strAutoCmp = prodctsDCCls.AutoCmpltPrdct();
            TextBox txtAuto = e.Control as TextBox;
            txtAuto.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtAuto.AutoCompleteSource = AutoCompleteSource.CustomSource;

            if (this.dgvDcNon.Columns[this.dgvDcNon.CurrentCell.ColumnIndex].Name == "itemDiscrip")
            {
                var name = new AutoCompleteStringCollection();
                name.AddRange(strAutoCmp);
                if (txtAuto != null)
                {
                    txtAuto.AutoCompleteCustomSource = name;
                }
            }
            else
            {
                txtAuto.AutoCompleteCustomSource = null;
            }

【问题讨论】:

  • 到底发生了什么?您仍然可以更改该列单元格中的值吗?
  • 是的。准确先生@yBee
  • 尝试使用列的索引,看看是否有区别。这应该可以正常工作。例如 dgvDcNon.Columns[1].ReadOnly = true;
  • 您确定您的代码中没有任何地方可以覆盖该值吗?
  • Yahh.. 没有覆盖。我确定

标签: c# datagridview readonly


【解决方案1】:

可能与数据源有关吗? 如果数据源是只读的,则无法更改。

已编辑

如果你为此使用数据库模型,EX:

Class Item
{
    public string itemId {get; set;}
    public string itemName {get; set;}
    public string itemDiscrip {get; set;}
    public int itemSize {get; set;}
    //whatever
}

尝试从您的代码中删除设置器:

public int itemSize {get; set;}

应该变成

public int itemSize {get;}

如果您有其他类型的数据源,请尝试使用 readonly 修饰符。

【讨论】:

  • 我应该将数据源设置为只读吗?如果如此......如何将只读设置为数据源。我的数据源是数据表
  • 这取决于您如何读取数据源。如果您有数据库模型,请尝试从该属性中删除设置器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2020-09-05
  • 2015-07-07
  • 2021-05-20
  • 1970-01-01
相关资源
最近更新 更多