添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

可以使用 GridColumnStylesCollection DataGridColumnStyle 对象(它们是 DataGridTableStyle 类的成员)的属性和方法以编程方式删除或隐藏 Windows 窗体 DataGrid 控件中的列。

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

如果应用程序不访问某些数据列,并且你不希望它们显示在数据网格中,那么可能一开始就没有必要将它们包含在数据源中。

以编程方式从 DataGrid 中删除列

  • 在窗体的声明区域中,声明 DataGridTableStyle 类的新实例。

  • DataGridTableStyle.MappingName 属性设置为数据源中要向其应用样式的表。 以下示例使用 DataGrid.DataMember 属性,该示例假定已设置该属性。

  • 将新的 DataGridTableStyle 对象添加到数据网格的表样式集合中。

  • 调用 DataGrid GridColumnStyles 集合的 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);  
    

    以编程方式隐藏 DataGrid 中的列

  • 在窗体的声明区域中,声明 DataGridTableStyle 类的新实例。

  • DataGridTableStyleMappingName 属性设置为数据源中要向其应用样式的表。 以下代码示例使用 DataGrid.DataMember 属性,该示例假定已设置该属性。

  • 将新的 DataGridTableStyle 对象添加到数据网格的表样式集合中。

  • 通过将其 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 控件添加表和列
  •