如何:在 Windows 窗体 DataGrid 控件中删除或隐藏列

提示

DataGridView 控件取代了 DataGrid 控件并添加了功能;但是,可以选择保留 DataGrid 控件以实现向后兼容并供将来使用。 有关更多信息,请参见 Windows 窗体 DataGridView 控件和 DataGrid 控件之间的区别

使用 GridColumnStylesCollectionDataGridColumnStyle 对象(属于 DataGridTableStyle 类的成员)的属性和方法,可以通过编程方式删除或隐藏 Windows 窗体 DataGrid 控件中的列。

已删除或隐藏的列仍存在于网格绑定到的数据源中,而且仍然可以通过编程方式访问。 只是在数据网格中无法再看到它们。

提示

如果您的应用程序不访问某些数据列,而且您不希望在数据网格中显示它们,那么,一开始就不必将它们包括在数据源中。

以编程方式从 DataGrid 中删除列

  1. 在窗体的声明区域中声明 DataGridTableStyle 类的新实例。

  2. DataGridTableStyle.MappingName 属性设置为希望应用样式的数据源中的表。 下面的示例使用 DataGrid.DataMember 属性(假设已经设置了它)。

  3. 向数据网格的表样式集合中添加新的 DataGridTableStyle 对象。

  4. 调用 DataGridGridColumnStyles 集合的 RemoveAt 方法,指定要删除列的列索引。

    ' Declare a new DataGridTableStyle in the
    ' declarations area of your form.
    Dim ts As DataGridTableStyle = New DataGridTableStyle()
    
    Sub DeleteColumn()
       ' Set the DataGridTableStyle.MappingName property
       ' to the table in the data source to map to.
       ts.MappingName = DataGrid1.DataMember
    
       ' Add it to the datagrid's TableStyles collection
       DataGrid1.TableStyles.Add(ts)
    
       ' Delete the first column (index 0)
       DataGrid1.TableStyles(0).GridColumnStyles.RemoveAt(0)
    End Sub
    
    // Declare a new DataGridTableStyle in the
    // declarations area of your form.
    DataGridTableStyle ts = new DataGridTableStyle();
    
    private void deleteColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;
    
       // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);
    
       // Delete the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles.RemoveAt(0);
    }
    

以编程方式隐藏数据网格中的列

  1. 在窗体的声明区域中声明 DataGridTableStyle 类的新实例。

  2. DataGridTableStyleMappingName 属性设置为希望应用样式的数据源中的表。 下面的代码示例使用 DataGrid.DataMember 属性(假设已经设置了它)。

  3. 向数据网格的表样式集合中添加新的 DataGridTableStyle 对象。

  4. 通过将列的 Width 属性设置为 0,并指定要隐藏的列的列索引来隐藏列。

    ' Declare a new DataGridTableStyle in the
    ' declarations area of your form.
    Dim ts As DataGridTableStyle = New DataGridTableStyle()
    
    Sub HideColumn()
       ' Set the DataGridTableStyle.MappingName property
       ' to the table in the data source to map to.
       ts.MappingName = DataGrid1.DataMember
    
       ' Add it to the datagrid's TableStyles collection
       DataGrid1.TableStyles.Add(ts)
    
       ' Hide the first column (index 0)
       DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0
    End Sub
    
    // Declare a new DataGridTableStyle in the
    // declarations area of your form.
    DataGridTableStyle ts = new DataGridTableStyle();
    
    private void hideColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;
    
       // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);
    
       // Hide the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 0;
    }
    

请参见

任务

如何:在运行时更改 Windows 窗体 DataGrid 控件中显示的数据

如何:向 Windows 窗体 DataGrid 控件添加表和列