C# Can';datagridview中的t控件只读属性

C# Can';datagridview中的t控件只读属性,c#,datagridview,readonly,C#,Datagridview,Readonly,我在C#应用程序中使用datagridview。我只需要一个特定的列是可编辑的,而不是整个列。最后一行必须为只读 我在design中将datagridview只读属性设置为false。然后我使用代码序列将所有其他列转换为只读: dataGridView1.Columns[“Col1”]。ReadOnly=true; dataGridView1.Columns[“Col2”].ReadOnly=true; dataGridView1.Columns[“Col3”].ReadOnly=true; d

我在C#应用程序中使用datagridview。我只需要一个特定的列是可编辑的,而不是整个列。最后一行必须为只读

我在design中将datagridview只读属性设置为false。然后我使用代码序列将所有其他列转换为只读:

dataGridView1.Columns[“Col1”]。ReadOnly=true;
dataGridView1.Columns[“Col2”].ReadOnly=true;
dataGridView1.Columns[“Col3”].ReadOnly=true;
dataGridView1.Columns[“Col4”].ReadOnly=true

datagridview有5列。此时,只有Col5是可编辑的。但我需要最后一行为只读,因此我尝试:

dataGridView1.Rows[dataGridView1.Rows.Count - 1].ReadOnly = true;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Col5"].ReadOnly = true;
这不起作用,整个Col5仍然是可编辑的。所以我试着:

dataGridView1.Rows[dataGridView1.Rows.Count - 1].ReadOnly = true;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Col5"].ReadOnly = true;
那也不管用。整个Col5仍然是可编辑的

我如何解决这个问题?

方法1: 数据绑定完成后,可以更改行(或单元格)的只读状态:

dataGridView1.DataBindingComplete += dataGridView1_DataBindingComplete;
以及

方法2: 如果行是最后一行,则可以取消对任何单元格的编辑:

dataGridView1.CellBeginEdit += dataGridView1_CellBeginEdit;


尝试将design中的datagridview.readonly属性设置为true。最后一列只读错误。在最后一行之后为readonly true