How To Hide a Column in a Windows Form DataGrid

Summary

This step-by-step article describes how to hide a column programatically in a Windows Form DataGrid by manipulating the properties of the DataGrid object.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
  • Microsoft Visual Studio .Net installed on a computer running a compatible Microsoft Windows operating system.
  • An available instance of Microsoft SQL Server or an available Microsoft Access database for testing.
This article assumes that you are familiar with the following topics:
  • Visual Basic .NET Windows Forms applications
  • ADO.NET coding and data binding

Description of the Technique

The individual columns of a displayed table are not exposed directly as properties of the DataGrid object. To manipulate the columns individually, you must use the DataGridTableStyle class, which is a member of the DataGrid object.

In this sample, the Load event procedure retrieves all of the columns for the Products table. However, the code in the Load event procedure hides the QuantityPerUnit column from the grid. The column is hidden only in the grid and remains a part of the dataset.

Sample

  1. Open Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  3. Add a DataGrid control to Form1.
  4. Add the following line of code in the General Declarations section of Form1:
    Imports System.Data.SqlClient
  5. Paste the following code in the Load event procedure for Form1.

    NOTE: Make sure to modify the connection string for your SQL Server instance.
    Dim cn As New SqlConnection("Data Source=MyServer;User Id=MyUser;Password=MyPassword;Initial Catalog=Northwind")
    cn.Open()
    Dim da As New SqlDataAdapter("SELECT * FROM Products", cn)
    Dim ds As New DataSet()
    da.FillSchema(ds, SchemaType.Source, "Products")
    da.Fill(ds, "Products")
    DataGrid1.SetDataBinding(ds, "Products")
    ' Code to hide column.
    Dim ts As New DataGridTableStyle()
    ts.MappingName = "Products"
    DataGrid1.TableStyles.Add(ts)
    DataGrid1.SetDataBinding(ds, "Products")
    DataGrid1.TableStyles("Products").GridColumnStyles.Remove(DataGrid1.TableStyles("Products").GridColumnStyles("QuantityPerUnit"))
  6. Run the project. Note that the QuantityPerUnit column does not appear in the DataGrid at runtime although the SELECT statement includes all of the columns from the table.
NOTE: Alternatively, you can use the ColumnMapping property of the Columns collection of the DataTable. When you set this property to Hidden, the column is not displayed. The code to paste in the form Load event to use this technique resembles the following:
Dim cn As New SqlConnection("Data Source=MyServer;User Id=MyUser;Password=MyPassword;Initial Catalog=Northwind")
cn.Open()
Dim da As New SqlDataAdapter("Select * From Products", cn)
Dim ds As New DataSet()
da.FillSchema(ds, SchemaType.Source, "Products")
da.Fill(ds, "Products")
' Next line hides column.
ds.Tables("Products").Columns("QuantityPerUnit").ColumnMapping = MappingType.Hidden
DataGrid1.SetDataBinding(ds, "Products")

Pitfalls

  • If you want to toggle between hiding a column and showing a column at design time, you only need to add the DataGridTableStyle once.
  • To show the column, set the Width property of GridColumnsStyles to a nonzero value; for example:
    DataGrid1.TableStyles("Products").GridColumnStyles("QuantityPerUnit").Width = 75

REFERENCES

If you bind the grid at design time, you can use the builder from the property sheet to hide or show columns. This method is described further in the Visual Studio .NET documentation, which is available at the following MSDN Web site: You can find additional documentation about the DataGrid classes at the following MSDN Web sites:
Proprietăți

ID articol: 317951 - Ultima examinare: 23 mar. 2009 - Revizie: 1

Feedback