如何:隱藏 Windows Form DataGridView 控制項中的資料行

有時候您會想要只顯示 Windows Form DataGridView 控制項中某些可用的資料行。 例如,您可能會想要對具有管理認證的使用者顯示員工薪資資料行,而對其他使用者隱藏該資料行。 或者,您可能會想要將控制項繫結至包含許多資料行的資料來源,但您只想要顯示其中部分資料行。 在此情況下,您通常會移除不想顯示的資料行,而不是加以隱藏。

DataGridView 控制項中,資料行的 Visible 屬性值會決定是否要顯示該資料行。

在 Visual Studio 中會支援這項工作。 另請參閱 如何:使用設計工具 隱藏 Windows Forms DataGridView 控制項中的資料行。

以程式設計方式隱藏資料行

  • DataGridViewColumn.Visible 屬性設為 false。 若要隱藏在資料繫結期間自動產生的 CustomerID 資料行,請將下列程式碼範例放在 DataBindingComplete 事件處理常式中。

    this.dataGridView1.Columns["CustomerID"].Visible = false;
    
    Me.dataGridView1.Columns("CustomerID").Visible = False
    

編譯程式碼

這個範例需要:

另請參閱