共用方式為


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

更新:2007 年 11 月

有時候您希望防止使用者輸入新的資料資料列,或刪除 DataGridView 控制項中的現有資料列。AllowUserToAddRows 屬性會指示新資料錄的資料列是否出現在控制項下方,而 AllowUserToDeleteRows 屬性則指示是否能夠移除資料列。下列程式碼範例會使用這些屬性,並且設定 ReadOnly 屬性,讓整個控制項都是唯讀。

Visual Studio 中會支援這項工作。

範例

Private Sub MakeReadOnly()

    With dataGridView1
        .AllowUserToAddRows = False
        .AllowUserToDeleteRows = False
        .ReadOnly = True
    End With

End Sub
private void MakeReadOnly()
{
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;
    dataGridView1.ReadOnly = true;
}

編譯程式碼

這項範例需要:

請參閱

參考

DataGridView

DataGridView.AllowUserToAddRows

DataGridView.ReadOnly

DataGridView.AllowUserToAddRows

DataGridView.AllowUserToDeleteRows

其他資源

Windows Form DataGridView 控制項中的基本資料行、資料列和儲存格功能