有时,你需要阻止用户输入新数据行或删除 DataGridView 控件中的现有行。 该 AllowUserToAddRows 属性指示控件底部是否存在新记录的行,而 AllowUserToDeleteRows 该属性指示是否可以删除行。 下面的代码示例使用这些属性,并设置属性 ReadOnly 以使控件完全只读。
Visual Studio 中支持此任务。 另请参阅 如何:使用设计器防止在 Windows 窗体 DataGridView 控件中添加和删除行。
示例:
private void MakeReadOnly()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.ReadOnly = true;
}
Private Sub MakeReadOnly()
With dataGridView1
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.ReadOnly = True
End With
End Sub
编译代码
此示例需要:
名为 DataGridView 的
dataGridView1控件。对 System 和 System.Windows.Forms 程序集的引用。