如何:使 Windows Form DataGridView 控制項中的資料行成為唯讀
並非所有資料都是可編輯的。 在 DataGridView 控制項中,資料行 ReadOnly 屬性值會決定使用者是否能編輯該資料行中的儲存格。 如需如何讓控制項完全唯讀的資訊,請參閱 How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control 。
在 Visual Studio 中會支援這項工作。 另請參閱 如何:使用設計 工具在 Windows Forms DataGridView 控制項中將資料行設為唯讀。
以程式設計方式將資料行設為唯讀
將 DataGridViewColumn.ReadOnly 屬性設為
true
。dataGridView1.Columns["CompanyName"].ReadOnly = true;
dataGridView1.Columns("CompanyName").ReadOnly = True
編譯程式碼
這個範例需要:
名為
dataGridView1
的 DataGridView 控制項,包含名為CompanyName
的資料行。System 和 System.Windows.Forms 組件的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應