有時候您會想要只顯示 Windows Form DataGridView 控制項中的一些可用資料行。 例如,您可能想要對具有管理認證的使用者顯示員工薪資資料行,但對其他使用者隱藏此項資訊; 或者,也可能想將控制項繫結至包含許多資料行的資料來源,但只想要顯示其中某些資料行。 在這種情況下,通常您會將不想顯示的資料行移除,而非加以隱藏。
在 DataGridView 控制項中,資料行的 Visible 屬性值會決定是否顯示該資料行。
Visual Studio 中會支援這項工作。 如需詳細資訊,請參閱HOW TO:使用設計工具隱藏 Windows Form DataGridView 控制項中的資料行 和 HOW TO:使用設計工具隱藏 Windows Form DataGridView 控制項中的資料行 和 HOW TO:使用設計工具隱藏 Windows Form DataGridView 控制項中的資料行 和 HOW TO:使用設計工具隱藏 Windows Form DataGridView 控制項中的資料行.
若要以程式設計方式隱藏欄位
將 DataGridViewColumn.Visible 屬性設定為 false。 若要隱藏在資料繫結 (Data Binding) 期間自動產生的 CustomerID 資料行,請將下列程式碼範例放入 DataBindingComplete 事件處理常式中。
Me.dataGridView1.Columns("CustomerID").Visible = Falsethis.dataGridView1.Columns["CustomerID"].Visible = false;
編譯程式碼
這項範例需要:
名為 dataGridView1 的 DataGridView 控制項,包含名為 CustomerID 的資料行。
System 和 System.Windows.Forms 組件的參考。
請參閱
工作
HOW TO:移除 Windows Form DataGridView 控制項中自動產生的資料行
HOW TO:變更 Windows Form DataGridView 控制項資料行的順序