DataGridView.AutoSizeRowsMode 属性


获取或设置一个值,该值指示如何确定行高。Gets or sets a value indicating how row heights are determined.

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


一个 DataGridViewAutoSizeRowsMode 值,指示大小调整模式。A DataGridViewAutoSizeRowsMode value indicating the sizing mode. 默认值为 NoneThe default is None.


当设置此属性时,指定的值不是有效的 DataGridViewAutoSizeRowsMode 值。The specified value when setting this property is not a valid DataGridViewAutoSizeRowsMode value.

当设置此属性时,指定的值是 AllHeadersDisplayedHeaders,且行标题被隐藏。The specified value when setting this property is AllHeaders or DisplayedHeaders and row headers are hidden.


下面的代码示例演示如何将行设置为基于行标题和所有列的内容自动调整大小。The following code example demonstrates how to set the row to automatically resize based on the contents of the row headers and all of the columns. 此代码示例摘自 [How 中提供的一个更大的示例:当 Windows 窗体 DataGridView 控件 @ no__t 中的内容更改时,自动调整单元格的大小。This code example is part of a larger example provided in How to: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control.

void AutoSizeRowsMode( Object^ /*sender*/, EventArgs^ /*es*/ )
   dataGridView1->AutoSizeRowsMode = DataGridViewAutoSizeRowsMode::AllCells;
private void AutoSizeRowsMode(Object sender, EventArgs es)
    dataGridView1.AutoSizeRowsMode =
Private Sub AutoSizeRowsMode(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button7.Click

    DataGridView1.AutoSizeRowsMode = _

End Sub


此属性可让你配置控件,以便在内容发生更改时自动调整行高以适应其内容。This property lets you configure the control so that row heights are automatically adjusted to fit their contents whenever the contents change. 如果启用 WrapMode,则当列宽发生变化时也会调整行高。If WrapMode is enabled, row heights are also adjusted whenever column widths change. 某些大小调整模式允许将大小调整限制为当前显示的行,以便提高性能。Some sizing modes let you limit the size adjustment to the currently displayed rows in order to increase performance.

只有 Visible 属性值为 true 的行自动调整大小,更改行的可见性不会导致调整大小。Only rows with a Visible property value of true are resized automatically, and changing the visibility of a row does not cause resizing to occur. 此外,如果将行设置为自动调整大小,则用户无法用鼠标调整行高。Additionally, when rows are set to automatically resize, the user cannot adjust the row heights with the mouse.

自动调整大小的行的 @no__t 值0会在布局期间重新计算,以确定所使用字体的大小。The Height value of an automatically sized row is recalculated during layout to account for the size of the font that is used. 若要获取正确的 Height,应在窗体的 Layout 事件而不是在窗体的构造函数中检索值。To get the correct Height, you should retrieve the value in the form's Layout event instead of in the form's constructor.

若要以编程方式调整行高,请使用 AutoResizeRow 或 @no__t 方法,或设置行 Height 属性。To adjust row heights programmatically, use the AutoResizeRow or AutoResizeRows methods, or set the row Height property.

若要在调整行大小时将单元格内容换行到多行,对单元格有效的单元格样式必须将 WrapMode 属性值设置 @no__t。For cell contents to wrap onto multiple lines when a row is resized, the cell style in effect for the cell must have a WrapMode property value of True.

有关基于内容的自动调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.