This documentation is archived and is not being maintained.

DataGridView.ColumnHeadersVisible Property

Gets or sets a value indicating whether the column header row is displayed.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public bool ColumnHeadersVisible { get; set; }

Property Value

Type: System.Boolean
true if the column headers are displayed; otherwise, false. The default is true.


The specified value when setting this property is false and one or more columns have an InheritedAutoSizeMode property value of ColumnHeader.

If the ColumnHeadersVisible property is false, a column whose Resizable property is set to true can still be resized in the user interface (UI) by clicking with the mouse cursor anywhere along the right border of the column to be resized.

The following code example populates an unbound DataGridView control and sets the ColumnHeadersVisible property to true to ensure that column headers are displayed.

    private void InitializeDataGridView()
        // Create an unbound DataGridView by declaring a column count.
        dataGridView1.ColumnCount = 4;
        dataGridView1.ColumnHeadersVisible = true;

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

        columnHeaderStyle.BackColor = Color.Beige;
        columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
        dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

        // Set the column header names.
        dataGridView1.Columns[0].Name = "Recipe";
        dataGridView1.Columns[1].Name = "Category";
        dataGridView1.Columns[2].Name = "Main Ingredients";
        dataGridView1.Columns[3].Name = "Rating";

        // Populate the rows.
        string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef",
            "**" };
        string[] row2 = new string[] { "Key Lime Pie", "Dessert", 
            "lime juice, evaporated milk", "****" };
        string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish", 
            "pork chops, salsa, orange juice", "****" };
        string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad", 
            "black beans, brown rice", "****" };
        string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert", 
            "cream cheese", "***" };
        string[] row6 = new string[] { "Black Bean Dip", "Appetizer", 
            "black beans, sour cream", "***" };
        object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };

        foreach (string[] rowArray in rows)

    private void button1_Click(object sender, System.EventArgs e)
        // Resize the height of the column headers. 

        // Resize all the row heights to fit the contents of all non-header cells.

    private void InitializeContextMenu()
        // Create the menu item.
        ToolStripMenuItem getRecipe = new ToolStripMenuItem("Search for recipe", null,
            new System.EventHandler(ShortcutMenuClick));

        // Add the menu item to the shortcut menu.
        ContextMenuStrip recipeMenu = new ContextMenuStrip();

        // Set the shortcut menu for the first column.
        dataGridView1.Columns[0].ContextMenuStrip = recipeMenu;
        dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);

    private DataGridViewCell clickedCell;

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
	// If the user right-clicks a cell, store it for use by the shortcut menu. 
        if (e.Button == MouseButtons.Right)
            DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
            if (hit.Type == DataGridViewHitTestType.Cell)
                clickedCell =

    private void ShortcutMenuClick(object sender, System.EventArgs e)
        if (clickedCell != null)
            //Retrieve the recipe name.
            string recipeName = (string)clickedCell.Value;

            //Search for the recipe.
                "" + recipeName);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0