Microsoft KB Archive/317951

= How To Hide a Column in a Windows Form DataGrid =

Article ID: 317951

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q317951



IN THIS TASK
SUMMARY
 * Requirements
 * Description of the Technique
 * Sample
 * Pitfalls

REFERENCES



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(&quot;Data Source=MyServer;User Id=MyUser;Password=MyPassword;Initial Catalog=Northwind&quot;) cn.Open Dim da As New SqlDataAdapter(&quot;SELECT * FROM Products&quot;, cn) Dim ds As New DataSet da.FillSchema(ds, SchemaType.Source, &quot;Products&quot;) da.Fill(ds, &quot;Products&quot;) DataGrid1.SetDataBinding(ds, &quot;Products&quot;) ' Code to hide column. Dim ts As New DataGridTableStyle ts.MappingName = &quot;Products&quot; DataGrid1.TableStyles.Add(ts) DataGrid1.SetDataBinding(ds, &quot;Products&quot;) DataGrid1.TableStyles(&quot;Products&quot;).GridColumnStyles.Remove(DataGrid1.TableStyles(&quot;Products&quot;).GridColumnStyles(&quot;QuantityPerUnit&quot;))  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.</ol>

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(&quot;Data Source=MyServer;User Id=MyUser;Password=MyPassword;Initial Catalog=Northwind&quot;) cn.Open Dim da As New SqlDataAdapter(&quot;Select * From Products&quot;, cn) Dim ds As New DataSet da.FillSchema(ds, SchemaType.Source, &quot;Products&quot;) da.Fill(ds, &quot;Products&quot;) ' Next line hides column. ds.Tables(&quot;Products&quot;).Columns(&quot;QuantityPerUnit&quot;).ColumnMapping = MappingType.Hidden DataGrid1.SetDataBinding(ds, &quot;Products&quot;) back to the top

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

back to the top