DataGridViewCellStyle.Padding 属性

定义

获取或设置 DataGridViewCell 的边缘和它的内容之间的距离。Gets or sets the space between the edge of a DataGridViewCell and its content.

public:
 property System::Windows::Forms::Padding Padding { System::Windows::Forms::Padding get(); void set(System::Windows::Forms::Padding value); };
public System.Windows.Forms.Padding Padding { get; set; }
member this.Padding : System.Windows.Forms.Padding with get, set
Public Property Padding As Padding

属性值

Padding,它表示 DataGridViewCell 的边缘和它的内容之间的距离。A Padding that represents the space between the edge of a DataGridViewCell and its content.

示例

下面的代码示例演示如何在包含所有者描述的行的 DataGridView 控件中使用此属性。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. Padding 属性用于为此文本提供额外的空间。The Padding property is used to provide the extra space for this text. 在此示例中,所需的额外高度存储在一个名为 CUSTOM_CONTENT_HEIGHT的常量中。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. 有关完整的示例,请参阅如何:自定义 Windows 窗体 DataGridView 控件中的行的外观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 =
    Color.Transparent;

// Set the row height to accommodate the content that 
// spans multiple columns.
this.dataGridView1.RowTemplate.Height += CUSTOM_CONTENT_HEIGHT;
' Set a cell padding to provide space for the top of the focus 
' rectangle and for the content that spans multiple columns. 
Dim newPadding As New Padding(0, 1, 0, CUSTOM_CONTENT_HEIGHT)
Me.dataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding

' Set the selection background color to transparent so 
' the cell won't paint over the custom selection background.
Me.dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor = _
    Color.Transparent

' Set the row height to accommodate the normal cell content and the 
' content that spans multiple columns.
Me.dataGridView1.RowTemplate.Height += CUSTOM_CONTENT_HEIGHT

注解

设置 Padding 属性会影响绘制 DataGridViewCell 的编辑控件的位置。Setting the Padding property affects where the editing control of the DataGridViewCell is drawn. 例如,如果将某个 DataGridViewTextBoxCell 上的 Padding 设置为其 LeftTopRightBottom 属性值为10的 System.Windows.Forms.Padding,则将在该单元格的中间绘制该单元格的内容,并在该单元格的边缘与单元格的边缘之间绘制10个像素的空间。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.

适用于

另请参阅