Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (43 sloc) 5.26 KB
title dev_langs helpviewer_keywords ms.assetid
How to: Set Default Cell Styles for the Windows Forms DataGridView Control
DataGridView control [Windows Forms], cell styles
cells [Windows Forms], styles
data grids [Windows Forms], cell styles

How to: Set Default Cell Styles for the Windows Forms DataGridView Control

With the xref:System.Windows.Forms.DataGridView control, you can specify default cell styles for the entire control and for specific columns and rows. These defaults filter down from the control level to the column level, then to the row level, then to the cell level. If a particular xref:System.Windows.Forms.DataGridViewCellStyle property is not set at the cell level, the default property setting at the row level is used. If the property is also not set at the row level, the default column setting is used. Finally, if the property is also not set at the column level, the default xref:System.Windows.Forms.DataGridView setting is used. With this setting, you can avoid having to duplicate the property settings at multiple levels. At each level, simply specify the styles that differ from the levels above it. For more information, see Cell Styles in the Windows Forms DataGridView Control.

There is extensive support for this task in Visual Studio. Also see How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer.

To set the default cell styles programmatically

  1. Set the properties of the xref:System.Windows.Forms.DataGridViewCellStyle retrieved through the xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType property.

    [!code-csharpSystem.Windows.Forms.DataGridViewMisc#141] [!code-vbSystem.Windows.Forms.DataGridViewMisc#141]

  2. Create and initialize new xref:System.Windows.Forms.DataGridViewCellStyle objects for use by multiple rows and columns.

    [!code-csharpSystem.Windows.Forms.DataGridViewMisc#142] [!code-vbSystem.Windows.Forms.DataGridViewMisc#142]

  3. Set the DefaultCellStyle property of specific rows and columns.

    [!code-csharpSystem.Windows.Forms.DataGridViewMisc#143] [!code-vbSystem.Windows.Forms.DataGridViewMisc#143]


[!code-csharpSystem.Windows.Forms.DataGridViewMisc#140] [!code-vbSystem.Windows.Forms.DataGridViewMisc#140]

Compiling the Code

This example requires:

  • A xref:System.Windows.Forms.DataGridView control named dataGridView1.

  • References to the xref:System?displayProperty=nameWithType, xref:System.Drawing?displayProperty=nameWithType, and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.

Robust Programming

To achieve maximum scalability when you work with very large data sets, you should share xref:System.Windows.Forms.DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles, rather than set the style properties for individual elements separately. Additionally, you should create shared rows and access them by using the xref:System.Windows.Forms.DataGridViewRowCollection.SharedRow%2A?displayProperty=nameWithType property. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

See also

You can’t perform that action at this time.