DataGridViewAutoSizeColumnsMode 枚举

定义

定义用于指定如何调整列宽的值。Defines values for specifying how the widths of columns are adjusted.

public enum class DataGridViewAutoSizeColumnsMode
public enum DataGridViewAutoSizeColumnsMode
type DataGridViewAutoSizeColumnsMode = 
Public Enum DataGridViewAutoSizeColumnsMode
继承
DataGridViewAutoSizeColumnsMode

字段

AllCells 6

列宽调整到适合列中所有单元格(包括标头单元格)的内容。The column widths adjust to fit the contents of all cells in the columns, including header cells.

AllCellsExceptHeader 4

列宽调整到适合列中除标头单元格以外所有单元格的内容。The column widths adjust to fit the contents of all cells in the columns, excluding header cells.

ColumnHeader 2

列宽调整到适合列标头单元格的内容。The column widths adjust to fit the contents of the column header cells.

DisplayedCells 10

列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(包括标头单元格)的内容。The column widths adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, including header cells.

DisplayedCellsExceptHeader 8

列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(不包括标头单元格)的内容。The column widths adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, excluding header cells.

Fill 16

列宽调整到使所有列宽精确填充控件的显示区域,要求使用水平滚动的目的只是保持列宽大于 MinimumWidth 属性值。The column widths adjust so that the widths of all columns exactly fill the display area of the control, requiring horizontal scrolling only to keep column widths above the MinimumWidth property values. 相对列宽由相对 FillWeight 属性值决定。Relative column widths are determined by the relative FillWeight property values.

None 1

列宽不会自动调整。The column widths do not automatically adjust.

示例

下面的代码示例演示了如何在主/详细信息方案中使用此枚举,其中两个 DataGridView 控件在父/子关系中显示两个表中的数据。The following code example illustrates the use of this enumeration in a master/details scenario where two DataGridView controls display data from two tables in a parent/child relationship. 在此示例中,主控件的列大小调整模式为 "无",并且列宽以编程方式初始化以适应加载的值。In this example, the column sizing mode for the master control is None and the column widths are programmatically initialized to fit the loaded values. 详细信息控件设置为自动调整大小模式,以便每当值更改时(例如,当用户更改父表中的当前行时),列就会自动调整。The details control is set to an automatic sizing mode so that columns will adjust automatically whenever the values change (for example, when the user changes the current row in the parent table). 此示例是如何:使用两个 Windows 窗体 DataGridView 控件创建主窗体/详细信息窗体中提供的更大示例的一部分。This example is part of a larger example available in How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls.

private void Form1_Load(object sender, System.EventArgs e)
{
    // Bind the DataGridView controls to the BindingSource
    // components and load the data from the database.
    masterDataGridView.DataSource = masterBindingSource;
    detailsDataGridView.DataSource = detailsBindingSource;
    GetData();

    // Resize the master DataGridView columns to fit the newly loaded data.
    masterDataGridView.AutoResizeColumns();

    // Configure the details DataGridView so that its columns automatically
    // adjust their widths when the data changes.
    detailsDataGridView.AutoSizeColumnsMode = 
        DataGridViewAutoSizeColumnsMode.AllCells;
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load

    ' Bind the DataGridView controls to the BindingSource
    ' components and load the data from the database.
    masterDataGridView.DataSource = masterBindingSource
    detailsDataGridView.DataSource = detailsBindingSource
    GetData()

    ' Resize the master DataGridView columns to fit the newly loaded data.
    masterDataGridView.AutoResizeColumns()

    ' Configure the details DataGridView so that its columns automatically
    ' adjust their widths when the data changes.
    detailsDataGridView.AutoSizeColumnsMode = _
        DataGridViewAutoSizeColumnsMode.AllCells

End Sub

注解

DataGridView 控件可以调整其列的大小,以使其填充控件的可用水平宽度,或使其显示所有单元格或指定单元格的全部内容。The DataGridView control can resize its columns to make them fill the available horizontal width of the control or to make them display the full contents of all cells or of specified cells.

Fill 模式下,通过调整该模式中的所有列的大小来确定列的首选宽度,使控件中的所有可见列完全填充可用显示区域的水平宽度。With Fill mode, the preferred width of a column is determined by resizing all columns in that mode so that all visible columns in the control exactly fill the horizontal width of the available display area. 使用其他模式时,首选列宽是显示所有单元中的最大单元值或该列的单元子集(如当前显示的行中的单元子集)所需的最小宽度。With other modes, the preferred column width is the minimum width required to display the largest cell value from all cells or a subset of cells in that column, such as the subset of cells that are in currently displayed rows. 使用单元子集来确定新的宽度有助于避免在处理多行数据时出现性能损失。Using a subset of cells to determine the new width is useful to avoid a performance penalty when working with many rows of data.

基于内容的自动调整大小可防止用户用鼠标调整列宽。Content-based automatic resizing prevents users from adjusting column widths with the mouse. 但在填充模式下启用了用户大小调整。User resizing is enabled in fill mode, however.

此枚举由 DataGridView 控件 AutoSizeColumnsMode 属性和 AutoResizeColumns 方法使用。This enumeration is used by the DataGridView control AutoSizeColumnsMode property and AutoResizeColumns method.

有关调整大小模式的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about sizing modes, see Sizing Options in the Windows Forms DataGridView Control. 有关列填充模式的详细信息,请参阅Windows 窗体 DataGridView 控件中的列填充模式For more information about column fill mode in particular, see Column Fill Mode in the Windows Forms DataGridView Control.

适用于

另请参阅