Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在DataGridView中,当添加新行时,在更新列的true(c#.net)时,将列的ReadOnly属性改为false_C#_.net_Datagridview_Datatable_Readonly - Fatal编程技术网

在DataGridView中,当添加新行时,在更新列的true(c#.net)时,将列的ReadOnly属性改为false

在DataGridView中,当添加新行时,在更新列的true(c#.net)时,将列的ReadOnly属性改为false,c#,.net,datagridview,datatable,readonly,C#,.net,Datagridview,Datatable,Readonly,我已将2个datatable列的readonly属性设置为true List.Columns[0].ReadOnly = true; List.Columns[1].ReadOnly = true; 但我只希望在用户尝试更新时它们是只读的,用户可以向dataGridView添加新行,所以我希望在尝试添加新行时将readonly属性设置为false。我尝试在datagrid的CellDoubleClick事件上执行此操作,但它不会做任何事情,因为调用beginedit已经太晚了

我已将2个datatable列的readonly属性设置为true

    List.Columns[0].ReadOnly = true;
    List.Columns[1].ReadOnly = true;
但我只希望在用户尝试更新时它们是只读的,用户可以向dataGridView添加新行,所以我希望在尝试添加新行时将readonly属性设置为false。我尝试在datagrid的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;

任何想法

都必须使用cellbegin edit使单元格只读属性为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

       }
   }

我希望它能帮助您……

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

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; 
    }
}
重要的部分是检查该行是否为新行