如何:隐藏 Windows 窗体 DataGridView 控件中的列
有时,你会想仅显示在 Windows 窗体 DataGridView 控件中可用的某些列。 例如,你或许想对具有管理凭据的用户显示雇员的工资列,但却对其他用户隐藏。 或者,你或许想将控件绑定到包含若干列的数据源,但其中仅有一部分是你想显示的列。 这种情况下,你通常会移除不想显示的列,而不是隐藏它们。
在 DataGridView 控件,列的 Visible 属性值确定是否显示该列。
Visual Studio 中对此任务提供支持。 另请参阅如何:使用设计器隐藏 Windows 窗体 DataGridView 控件中的列。
若要以编程方式隐藏某一列
将 DataGridViewColumn.Visible 属性设置为
false
。 若要隐藏数据绑定时自动生成的CustomerID
列,请将下面的代码示例放置在 DataBindingComplete 事件处理程序中。this.dataGridView1.Columns["CustomerID"].Visible = false;
Me.dataGridView1.Columns("CustomerID").Visible = False
编译代码
此示例需要:
名为
dataGridView1
的 DataGridView 控件,其包含一个名为CustomerID
的列。对 System 和 System.Windows.Forms 程序集的引用。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈