This documentation is archived and is not being maintained.

DataGridViewColumn Class

Represents a column in a DataGridView control.

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

public class DataGridViewColumn : DataGridViewBand, 
	IComponent, IDisposable

The DataGridViewColumn type exposes the following members.

Public methodDataGridViewColumn()Initializes a new instance of the DataGridViewColumn class to the default state.
Public methodDataGridViewColumn(DataGridViewCell)Initializes a new instance of the DataGridViewColumn class using an existing DataGridViewCell as a template.

Public propertyAutoSizeModeGets or sets the mode by which the column automatically adjusts its width.
Public propertyCellTemplateGets or sets the template used to create new cells.
Public propertyCellTypeGets the run-time type of the cell template.
Public propertyContextMenuStripGets or sets the shortcut menu for the column. (Overrides DataGridViewBand.ContextMenuStrip.)
Public propertyDataGridViewGets the DataGridView control associated with this element. (Inherited from DataGridViewElement.)
Public propertyDataPropertyNameGets or sets the name of the data source property or database column to which the DataGridViewColumn is bound.
Public propertyDefaultCellStyleGets or sets the column's default cell style. (Overrides DataGridViewBand.DefaultCellStyle.)
Public propertyDefaultHeaderCellTypeGets or sets the run-time type of the default header cell. (Inherited from DataGridViewBand.)
Public propertyDisplayedGets a value indicating whether the band is currently displayed onscreen. (Inherited from DataGridViewBand.)
Public propertyDisplayIndexGets or sets the display order of the column relative to the currently displayed columns.
Public propertyDividerWidthGets or sets the width, in pixels, of the column divider.
Public propertyFillWeightGets or sets a value that represents the width of the column when it is in fill mode relative to the widths of other fill-mode columns in the control.
Public propertyFrozenGets or sets a value indicating whether a column will move when a user scrolls the DataGridView control horizontally. (Overrides DataGridViewBand.Frozen.)
Public propertyHasDefaultCellStyleGets a value indicating whether the DefaultCellStyle property has been set. (Inherited from DataGridViewBand.)
Public propertyHeaderCellGets or sets the DataGridViewColumnHeaderCell that represents the column header.
Protected propertyHeaderCellCoreGets or sets the header cell of the DataGridViewBand. (Inherited from DataGridViewBand.)
Public propertyHeaderTextGets or sets the caption text on the column's header cell.
Public propertyIndexGets the relative position of the band within the DataGridView control. (Inherited from DataGridViewBand.)
Public propertyInheritedAutoSizeModeGets the sizing mode in effect for the column.
Public propertyInheritedStyleGets the cell style currently applied to the column. (Overrides DataGridViewBand.InheritedStyle.)
Public propertyIsDataBoundGets a value indicating whether the column is bound to a data source.
Protected propertyIsRowGets a value indicating whether the band represents a row. (Inherited from DataGridViewBand.)
Public propertyMinimumWidthGets or sets the minimum width, in pixels, of the column.
Public propertyNameGets or sets the name of the column.
Public propertyReadOnlyGets or sets a value indicating whether the user can edit the column's cells. (Overrides DataGridViewBand.ReadOnly.)
Public propertyResizableGets or sets a value indicating whether the column is resizable. (Overrides DataGridViewBand.Resizable.)
Public propertySelectedGets or sets a value indicating whether the band is in a selected user interface (UI) state. (Inherited from DataGridViewBand.)
Public propertySiteInfrastructure. Gets or sets the site of the column.
Public propertySortModeGets or sets the sort mode for the column.
Public propertyStateGets the user interface (UI) state of the element. (Inherited from DataGridViewElement.)
Public propertyTagGets or sets the object that contains data to associate with the band. (Inherited from DataGridViewBand.)
Public propertyToolTipTextGets or sets the text used for ToolTips.
Public propertyValueTypeGets or sets the data type of the values in the column's cells.
Public propertyVisibleGets or sets a value indicating whether the column is visible. (Overrides DataGridViewBand.Visible.)
Public propertyWidthGets or sets the current width of the column.

Public methodCloneCreates an exact copy of this band. (Overrides DataGridViewBand.Clone().)
Public methodDispose()Releases all resources used by the DataGridViewBand. (Inherited from DataGridViewBand.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the DataGridViewBand and optionally releases the managed resources. (Overrides DataGridViewBand.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeReleases the resources associated with the band. (Inherited from DataGridViewBand.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetPreferredWidthCalculates the ideal width of the column based on the specified criteria.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnDataGridViewChangedCalled when the band is associated with a different DataGridView. (Inherited from DataGridViewBand.)
Protected methodRaiseCellClickRaises the DataGridView.CellClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellContentClickRaises the DataGridView.CellContentClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellContentDoubleClickRaises the DataGridView.CellContentDoubleClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellValueChangedRaises the DataGridView.CellValueChanged event. (Inherited from DataGridViewElement.)
Protected methodRaiseDataErrorRaises the DataGridView.DataError event. (Inherited from DataGridViewElement.)
Protected methodRaiseMouseWheelRaises the Control.MouseWheel event. (Inherited from DataGridViewElement.)
Public methodToStringGets a string that describes the column. (Overrides DataGridViewBand.ToString().)

Public eventDisposedOccurs when the DataGridViewColumn is disposed.

The DataGridViewColumn class represents a logical column in a DataGridView control. You can retrieve columns through the Columns collection of the control.

Unlike a DataGridViewRow, which contains the actual collection of cells in a DataGridView, DataGridViewColumn is used mainly to adjust the appearance and behavior of the column user interface (UI), such as column width and cell style. For more information about cell styles, see Cell Styles in the Windows Forms DataGridView Control.

Types that derive from DataGridViewColumn typically initialize the CellTemplate property to a new instance of a related type derived from the DataGridViewCell class. Any column properties that relate to the appearance or behavior of individual cells are wrappers for the corresponding properties of the template cell. Changing one of these properties on the column automatically changes the value on the cell template and on all cells in the column. To override the specified value for individual cells, set the cell values after you set the column value.

Notes to Inheritors

When you derive from DataGridViewColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell.

The following code example creates a Windows Form with a DataGridView and a set of buttons. Each button label describes an operation related to a DataGridViewColumn property, such as swapping the first and last column (using the DisplayIndex property) or changing the text of a column header (using the HeaderText property). Clicking a button changes the associated property of the DataGridViewColumn.

using System.Windows.Forms;
using System;
using System.Drawing;

public class DataGridViewColumnDemo : Form
    #region "set up form"
    public DataGridViewColumnDemo()

        AddButton(Button1, "Reset",
            new EventHandler(ResetToDisorder));
        AddButton(Button2, "Change Column 3 Header",
            new EventHandler(ChangeColumn3Header));
        AddButton(Button3, "Change Meatloaf Recipe",
            new EventHandler(ChangeMeatloafRecipe));


    DataGridView dataGridView;
    Button Button1 = new Button();
    Button Button2 = new Button();
    Button Button3 = new Button();
    Button Button4 = new Button();
    Button Button5 = new Button();
    Button Button6 = new Button();
    Button Button7 = new Button();
    Button Button8 = new Button();
    Button Button9 = new Button();
    Button Button10 = new Button();
    FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();

    private void InitializeComponent()
        FlowLayoutPanel1.Location = new Point(454, 0);
        FlowLayoutPanel1.AutoSize = true;
        FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
        FlowLayoutPanel1.Name = "flowlayoutpanel";
        ClientSize = new System.Drawing.Size(614, 360);
        Text = this.GetType().Name;
        AutoSize = true;

    #region "set up DataGridView"

    private string thirdColumnHeader = "Main Ingredients";
    private string boringMeatloaf = "ground beef";
    private string boringMeatloafRanking = "*";
    private bool boringRecipe;
    private bool shortMode;

    private void InitializeDataGridView()
        dataGridView = new System.Windows.Forms.DataGridView();
        dataGridView.Size = new Size(300, 200);

        // Create an unbound DataGridView by declaring a
        // column count.
        dataGridView.ColumnCount = 4;

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();
        columnHeaderStyle.BackColor = Color.Aqua;
        columnHeaderStyle.Font =
            new Font("Verdana", 10, FontStyle.Bold);
        dataGridView.ColumnHeadersDefaultCellStyle =

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


        // Populate the rows.
        string[] row1 = new string[]{"Meatloaf", 
                                        "Main Dish", boringMeatloaf, boringMeatloafRanking};
        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)

        shortMode = false;
        boringRecipe = true;

    private void AddButton(Button button, string buttonLabel,
        EventHandler handler)
        button.TabIndex = FlowLayoutPanel1.Controls.Count;
        button.Text = buttonLabel;
        button.AutoSize = true;
        button.Click += handler;

    private void ResetToDisorder(object sender, System.EventArgs e)

    private void ChangeColumn3Header(object sender,
        System.EventArgs e)
        Toggle(ref shortMode);
        if (shortMode)
        { dataGridView.Columns[2].HeaderText = "S"; }
        { dataGridView.Columns[2].HeaderText = thirdColumnHeader; }

    private static void Toggle(ref bool toggleThis)
        toggleThis = !toggleThis;

    private void ChangeMeatloafRecipe(object sender,
        System.EventArgs e)
        Toggle(ref boringRecipe);
        if (boringRecipe)
            SetMeatloaf(boringMeatloaf, boringMeatloafRanking);
            string greatMeatloafRecipe =
                "1 lb. lean ground beef, " +
                "1/2 cup bread crumbs, 1/4 cup ketchup," +
                "1/3 tsp onion powder, " +
                "1 clove of garlic, 1/2 pack onion soup mix " +
                " dash of your favorite BBQ Sauce";
            SetMeatloaf(greatMeatloafRecipe, "***");

    private void SetMeatloaf(string recipe, string rating)
        dataGridView.Rows[0].Cells[2].Value = recipe;
        dataGridView.Rows[0].Cells[3].Value = rating;

    #region "demonstration code"
    private void PostColumnCreation()

        dataGridView.CellMouseEnter +=
        dataGridView.AutoSizeColumnModeChanged +=

    private string criteriaLabel = "Column 3 sizing criteria: ";
    private void AddCriteriaLabel()
            criteriaLabel +
            dataGridView.Columns[2].AutoSizeMode.ToString() +

    private void AddContextLabel()
        string labelName = "label";
            "Use shortcut menu to change cell color.");

    private void AddLabelToPanelIfNotAlreadyThere(
        string labelName, string labelText)
        Label label;
        if (FlowLayoutPanel1.Controls[labelName] == null)
            label = new Label();
            label.AutoSize = true;
            label.Name = labelName;
            label.BackColor = Color.Bisque;
            label = (Label)FlowLayoutPanel1.Controls[labelName];
        label.Text = labelText;

    private void CustomizeCellsInThirdColumn()
        int thirdColumn = 2;
        DataGridViewColumn column =
        DataGridViewCell cell = new DataGridViewTextBoxCell();

        cell.Style.BackColor = Color.Wheat;
        column.CellTemplate = cell;

    ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem();

    private void AddContextMenu()
        toolStripItem1.Text = "Redden";
        toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
        ContextMenuStrip strip = new ContextMenuStrip();
        foreach (DataGridViewColumn column in dataGridView.Columns)

            column.ContextMenuStrip = strip;

    private DataGridViewCellEventArgs mouseLocation;

    // Change the cell's color.
    private void toolStripItem1_Click(object sender, EventArgs args)
            = Color.Red;

    // Deal with hovering over a cell.
    private void dataGridView_CellMouseEnter(object sender,
        DataGridViewCellEventArgs location)
        mouseLocation = location;

    private void SetDefaultCellInFirstColumn()
        DataGridViewColumn firstColumn = dataGridView.Columns[0];
        DataGridViewCellStyle cellStyle =
            new DataGridViewCellStyle();
        cellStyle.BackColor = Color.Thistle;

        firstColumn.DefaultCellStyle = cellStyle;

    private void ToolTips()
        DataGridViewColumn firstColumn = dataGridView.Columns[0];
        DataGridViewColumn thirdColumn = dataGridView.Columns[2];
        firstColumn.ToolTipText =
            "This column uses a default cell.";
        thirdColumn.ToolTipText =
            "This column uses a template cell." +
            " Style changes to one cell apply to all cells.";

    private void AddAdditionalButtons()
        AddButton(Button4, "Set Minimum Width of Column Two",
            new EventHandler(Button4_Click));
        AddButton(Button5, "Set Width of Column One",
            new EventHandler(Button5_Click));
        AddButton(Button6, "Autosize Third Column",
            new EventHandler(Button6_Click));
        AddButton(Button7, "Add Thick Vertical Edge",
            new EventHandler(Button7_Click));
        AddButton(Button8, "Style and Number Columns",
            new EventHandler(Button8_Click));
        AddButton(Button9, "Change Column Header Text",
            new EventHandler(Button9_Click));
        AddButton(Button10, "Swap First and Last Columns",
            new EventHandler(Button10_Click));

    private void AdjustDataGridViewSizing()
        dataGridView.ColumnHeadersHeightSizeMode = 

    //Set the minimum width.
    private void Button4_Click(object sender,
        System.EventArgs e)
        DataGridViewColumn column = dataGridView.Columns[1];
        column.MinimumWidth = 40;

    // Set the width.
    private void Button5_Click(object sender, System.EventArgs e)
        DataGridViewColumn column = dataGridView.Columns[0];
        column.Width = 60;

    // AutoSize the third column.
    private void Button6_Click(object sender,
        System.EventArgs e)
        DataGridViewColumn column = dataGridView.Columns[2];
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

    // Set the vertical edge.
    private void Button7_Click(object sender,
        System.EventArgs e)
        int thirdColumn = 2;
        DataGridViewColumn column =
        column.DividerWidth = 10;

    // Style and number columns.
    private void Button8_Click(object sender,
        EventArgs args)
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.Alignment =
        style.ForeColor = Color.IndianRed;
        style.BackColor = Color.Ivory;

        foreach (DataGridViewColumn column in dataGridView.Columns)
            column.HeaderCell.Value = column.Index.ToString();
            column.HeaderCell.Style = style;

    // Change the text in the column header.
    private void Button9_Click(object sender,
        EventArgs args)
        foreach (DataGridViewColumn column in dataGridView.Columns)

            column.HeaderText = String.Concat("Column ",

    // Swap the last column with the first.
    private void Button10_Click(object sender, EventArgs args)
        DataGridViewColumnCollection columnCollection = dataGridView.Columns;

        DataGridViewColumn firstVisibleColumn =
        DataGridViewColumn lastVisibleColumn =
                DataGridViewElementStates.Visible, DataGridViewElementStates.None);

        int firstColumn_sIndex = firstVisibleColumn.DisplayIndex;
        firstVisibleColumn.DisplayIndex = lastVisibleColumn.DisplayIndex;
        lastVisibleColumn.DisplayIndex = firstColumn_sIndex;

    // Updated the criteria label.
    private void dataGridView_AutoSizeColumnModeChanged(object sender,
        DataGridViewAutoSizeColumnModeEventArgs args)
            Text = criteriaLabel
            + args.Column.AutoSizeMode.ToString();

    public static void Main()
        Application.Run(new DataGridViewColumnDemo());


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.