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.
back to the top
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
back to the top
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.
back to the top
Sample
- Open Visual Studio .NET.
- Create a new Windows Application project in Visual Basic
.NET. Form1 is added to the project by default.
- Add a DataGrid control to Form1.
- Add the following line of code in the General Declarations
section of Form1:
Imports System.Data.SqlClient
- 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"))
- 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")
back to the top
Pitfalls
back to the top
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:
back to the top