共用方式為


如何:防止在 Windows Form DataGridView 控制項中新增和刪除資料列

有時候您會想要防止使用者在您的 DataGridView 控制項中輸入新的資料列或刪除現有的資料列。 AllowUserToAddRows 屬性會指出新記錄的資料列是否出現在控制項的底部,而 AllowUserToDeleteRows 屬性會指出是否可以移除資料列。 下列程式碼範例會使用這些屬性,也將會設定 ReadOnly 屬性使控制項為完全唯讀。

在 Visual Studio 中會支援這項工作。 另請參閱 如何:使用設計 工具防止 Windows Forms 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

編譯程式碼

這個範例需要:

另請參閱