How to Hide DataGrid Columns

  • Print this article
DataGrids can hold a wide variety of data.
DataGrids can hold a wide variety of data.

Datagrids are controls that hold data in grids like a spreadsheet. They can be used in both Windows forms and websites. The DataGrid is much more than just a table; you can group, sort and filter your data or separate it into pages. With the proper data connections, you can use a DataGrid to add, edit and delete data as well. The content of the DataGrid control is made up of columns of data fields and rows of actual data. Columns can hold simple data or other controls like drop-down lists and check boxes. These columns can be modified at both design and run-time.

Instructions

  1. Hide a Column During Design

    • 1

      Right-click on the DataGrid and select "Property Builder."

    • 2

      Click on the "Columns" tab on the right-hand side of the window.

    • 3

      Find the desired column in the "Selected columns" list or add a new one from the "Available columns" list.

    • 4

      Clear the "Visible" check box in the column properties and click "OK."

    Hide a Column with Code

    • 5

      Hide the column by setting the column mapping to hidden within the data set. This example assumes that you have a data set object called dsMyDataSet that contains a table called TableName with a column named ColumnName. Copy the code below in the desired language to hide the column.

      Example in C#:
      dsMyDataSet.Tables["TableName"].Columns["ColumnName"].ColumnMapping = MappingType.Hidden

      Example in VB.NET:
      dsMyDataSet.Tables("TableName").Columns("ColumnName").ColumnMapping = MappingType.Hidden

    • 6

      Hide the column using DataGrid table styles, if the method above does not work for you.

    • 7

      Add a DataGridTableStyle object to your code and map it to the name of your table. You can copy the code below in the desired language to accomplish this task.

      Example in C#:
      DataGridTableStyle objTableStyle = new DataGridTableStyle();
      objTableStyle.MappingName = "TableName";

      Example in VB.NET:
      Dim objTableStyle As New DataGridTableStyle()
      objTableStyle.MappingName = "TableName"

    • 8

      Apply your style to the DataGrid that contains the column you wish to hide. Copy the code below.

      Example in C#:
      myDataGrid.TableStyles.Add(objTableStyle);

      Example in VB.NET:
      myDataGrid.TableStyles.Add(objTableStyle)

    • 9

      Remove the desired column from the table style. The code below can be used to remove the column.

      Example in C#:
      myDataGrid.TableStyles["TableName"].GridColumnStyles.Remove(myDataGrid.TableStyles["TableName"].GridColumnStyles["ColumnName"]);

      Example in VB.NET:
      myDataGrid.TableStyles("TableName").GridColumnStyles.Remove(myDataGrid.TableStyles("TableName").GridColumnStyles("ColumnName"))

Tips & Warnings

  • As of .NET 2.0, the DataGrid has been replaced by the DataView, which is a similar, but more robust, control.

  • To show a DataGrid column that was hidden using styles, set the grid column styles' width to something greater than zero.

Related Searches

References

Resources

  • Photo Credit number background image by kuhar from Fotolia.com

Comments

You May Also Like

Related Ads

Featured
View Mobile Site