【问题标题】:In DataGridView turn ReadOnly property of column to false when adding new row, on updating its true (c#.net)在 DataGridView 中,在添加新行时将列的 ReadOnly 属性设置为 false,更新其 true (c#.net)
【发布时间】:2011-10-19 16:38:11
【问题描述】:

我已将 2 个数据表列的只读属性设置为 true。

    List.Columns[0].ReadOnly = true;
    List.Columns[1].ReadOnly = true;

但我只希望它们在用户尝试更新时只读,用户可以向 dataGridView 添加新行,所以我想在尝试添加新行时将 readonly 属性设置为 false。我尝试在数据网格的 CellDoubleClick 事件上执行此操作,但它不会做任何事情,因为调用 beginedit 已经太晚了。

if(e.RowIndex == GridView.Rows.Count-1)
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = false;
            else
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = true;

任何想法

【问题讨论】:

    标签: c# .net datagridview datatable readonly


    【解决方案1】:

    您必须使用 cellbegin 编辑来使单元格只读属性为 true。 .

       private  void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
       {
           if (dataGridView1.Columns[e.ColumnIndex].Name == "ColName0")
           {
               // you can check whether the read only property of that cell is false or not
    
           }
       }
    

    希望对你有帮助……

    【讨论】:

      【解决方案2】:

      听起来您想要做的是将网格中的所有行设为只读,除非它们是新行,因此意味着无法编辑创建的行。如果这是正确的,那么您可以在 DataBindingComplete 事件期间将该行设置为只读,如下所示:

      dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
      
      void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
      {
          foreach (DataGridViewRow item in dataGridView1.Rows)
          {
              if (!item.IsNewRow)
                  item.ReadOnly = true; 
          }
      }
      

      重要的部分是检查该行是否是新行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-10
        • 2016-02-09
        • 2014-07-28
        • 1970-01-01
        相关资源
        最近更新 更多