列的ReadOnly属性设置为

列的ReadOnly属性设置为

本文介绍了在DataGridView中,在添加新行时将列的ReadOnly属性设置为false(更新其true时)(c#.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have set the readonly property of 2 datatable columns to true.

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

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

But i only want them to be read only when user is trying to update, User can add new rows to dataGridView so i want to turn the readonly property to false when trying to add new row. i tried doing this on CellDoubleClick event of the datagrid but it wont do anything as it is to late for the beginedit to be called.

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;

任何想法

推荐答案

您必须使用cellbegin编辑将单元格的readonly属性设为true。

you Have to use the cellbegin edit to make the cell readonly property to 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

       }
   }

我希望它将对您有帮助...

I hope it will helps you...

这篇关于在DataGridView中,在添加新行时将列的ReadOnly属性设置为false(更新其true时)(c#.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:16