如何:隐藏 DataGridView 控件中的列

更新:2007 年 11 月

您可以以编程方式或使用设计器隐藏 DataGridView 控件中的列。无论使用哪种方法,都需要将要隐藏的列的“Visible”属性设置为 false。

使用设计器隐藏 DataGridView 列

  1. 如何:使用 DataGridView 控件显示数据中的说明,使用 Northwind 数据库中的 Customers 表添加 DataGridView 控件。

  2. 单击“Columns”属性旁边的省略号 (…) 按钮。

    “编辑列”对话框随即打开。

  3. 在“编辑列”对话框中,选择第二列 (Company Name),然后在“绑定列属性”窗口中,将“Visible”属性设置为“False”。

  4. 单击“确定”关闭对话框。

  5. 按 F5 运行代码并验证“Company Name”列是否处于隐藏状态。

以编程方式隐藏 DataGridView 列

  1. 双击 DataGridView 控件上的列以添加默认的 CellContentClick 事件处理程序,然后添加以下代码。此代码会隐藏 DataGridView 控件中的第一列。

    this.customersDataGridView.Columns[0].Visible = false;
    
  2. 按 F5 运行代码。

  3. DataGridView 控件内部单击并验证“Customer ID”列是否处于隐藏状态。

请参见

任务

如何:使用 DataGridView 控件显示数据

如何:对 DataGridView 控件中的数据进行排序

其他资源

访问和显示数据