如何:防止在 Windows 窗体 DataGridView 控件中添加和删除行
有时想要阻止用户在 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
编译代码
此示例需要:
名为
dataGridView1
的 DataGridView 控件。对 System 和 System.Windows.Forms 程序集的引用。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈