DataGridViewCellStyle.Padding Property


Gets or sets the space between the edge of a DataGridViewCell and its content.

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

public Padding Padding { get; set; }

Property Value

Type: System.Windows.Forms.Padding

A Padding that represents the space between the edge of a DataGridViewCell and its content.

Setting the Padding property affects where the editing control of the DataGridViewCell is drawn. For example, if you set Padding on a DataGridViewTextBoxCell to a System.Windows.Forms.Padding that has a value of 10 for its Left, Top, Right, and Bottom properties, the content of the cell is drawn in the middle of the cell with a space of 10 pixels between the edges of the text box and the edges of the cell.

The following code example illustrates the use of this property in a DataGridView control that contains owner-drawn rows. Each row displays text that spans the entire width of the row underneath the regular cell content. The Padding property is used to provide the extra space for this text. In the example, the extra height required is stored in a constant called CUSTOM_CONTENT_HEIGHT.

This code example is part of a larger example. For the complete example, see How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// Set a cell padding to provide space for the top of the focus 
// rectangle and for the content that spans multiple columns. 
Padding newPadding = new Padding(0, 1, 0, CUSTOM_CONTENT_HEIGHT);
this.dataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding;

// Set the selection background color to transparent so 
// the cell won't paint over the custom selection background.
this.dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor =

// Set the row height to accommodate the content that 
// spans multiple columns.
this.dataGridView1.RowTemplate.Height += CUSTOM_CONTENT_HEIGHT;

.NET Framework
Available since 2.0
Return to top