Windows 窗体 DataGridView 控件中的大小调整选项Sizing Options in the Windows Forms DataGridView Control

DataGridView由于发生了许多不同的情况, 行、列和标头可能会改变大小。DataGridView rows, columns, and headers can change size as a result of many different occurrences. 下表显示了这些情况。The following table shows these occurrences.

匹配项Occurrence 描述Description
用户调整大小User resize 用户可以通过拖动或双击行、列或标题分隔线来调整大小。Users can make size adjustments by dragging or double-clicking row, column, or header dividers.
控件调整大小Control resize 在列填充模式下, 当控件的宽度发生变化时, 列宽将更改;例如, 当控件停靠在其父窗体上并且用户调整窗体的大小时。In column fill mode, column widths change when the control width changes; for example, when the control is docked to its parent form and the user resizes the form.
单元格值更改Cell value change 在基于内容的自动调整大小模式下, 调整大小以适应新的显示值。In content-based automatic sizing modes, sizes change to fit new display values.
方法调用Method call 基于编程内容的大小调整使你可以基于方法调用时的单元格值进行机会调整大小。Programmatic content-based resizing lets you make opportunistic size adjustments based on cell values at the time of the method call.
属性设置Property setting 你还可以设置特定的高度和宽度值。You can also set specific height and width values.

默认情况下, 将启用用户大小调整, 禁用自动调整大小, 并剪裁宽度超过其列的单元值。By default, user resizing is enabled, automatic sizing is disabled, and cell values that are wider than their columns are clipped.

下表显示了可用于调整默认行为或使用特定大小调整选项实现特定效果的方案。The following table shows scenarios that you can use to adjust the default behavior or to use specific sizing options to achieve particular effects.

应用场景Scenario 实现Implementation
使用列填充模式, 在占用整个控件宽度的列中显示大小类似的数据, 而不显示水平滚动条。Use column fill mode for displaying similarly sized data in a relatively small number of columns that occupy the entire width of the control without displaying the horizontal scroll bar. AutoSizeColumnsMode 属性设置为 FillSet the AutoSizeColumnsMode property to Fill.
使用具有不同大小的显示值的列填充模式。Use column fill mode with display values of varying sizes. AutoSizeColumnsMode 属性设置为 FillSet the AutoSizeColumnsMode property to Fill. 通过设置列FillWeight属性或在使用数据填充控件后调用控件AutoResizeColumns方法来初始化相对列宽。Initialize relative column widths by setting the column FillWeight properties or by calling the control AutoResizeColumns method after filling the control with data.
使用具有不同重要性的值的列填充模式。Use column fill mode with values of varying importance. AutoSizeColumnsMode 属性设置为 FillSet the AutoSizeColumnsMode property to Fill. 为必须MinimumWidth始终显示某些数据的列设置较大的值, 或者为特定列使用填充模式之外的调整大小选项。Set large MinimumWidth values for columns that must always display some of their data or use a sizing option other than fill mode for specific columns.
使用列填充模式可避免显示控件背景。Use column fill mode to avoid displaying the control background. 将最后一列的Fill 属性设置为,并对其他列使用其他调整大小选项。AutoSizeModeSet the AutoSizeMode property of the last column to Fill and use other sizing options for the other columns. 如果其他列使用太多的可用空间, 请设置MinimumWidth最后一列的属性。If the other columns use too much of the available space, set the MinimumWidth property of the last column.
显示固定宽度的列, 例如图标或 ID 列。Display a fixed-width column, such as an icon or ID column. 对于列None , Resizable将设置AutoSizeMode为和。 FalseSet AutoSizeMode to None and Resizable to False for the column. 通过设置Width属性或在使用数据填充控件后调用控件AutoResizeColumn方法来初始化其宽度。Initialize its width by setting the Width property or by calling the control AutoResizeColumn method after filling the control with data.
当单元格内容更改时自动调整大小, 以避免剪辑和优化空间的使用。Adjust sizes automatically whenever cell contents change to avoid clipping and to optimize the use of space. 将自动调整大小属性设置为一个值, 该值表示基于内容的大小调整模式。Set an automatic sizing property to a value that represents a content-based sizing mode. 若要避免在处理大量数据时出现性能损失, 请使用只计算显示行的调整大小模式。To avoid a performance penalty when working with large amounts of data, use a sizing mode that calculates displayed rows only.
调整大小以适合显示行中的值, 以避免在使用多行时出现性能下降。Adjust sizes to fit values in displayed rows to avoid performance penalties when working with many rows. 使用自动大小调整或编程大小调整模式枚举值。Use the appropriate sizing-mode enumeration values with automatic or programmatic resizing. 若要在滚动时调整大小以适应新显示的行中的值, 请在Scroll事件处理程序中调用调整大小方法。To adjust sizes to fit values in newly displayed rows while scrolling, call a resizing method in a Scroll event handler. 若要自定义用户双击大小, 以便仅显示的行中的值确定新大小, 请在RowDividerDoubleClickColumnDividerDoubleClick事件处理程序中调用调整大小方法。To customize user double-click resizing so that only values in displayed rows determine the new sizes, call a resizing method in a RowDividerDoubleClick or ColumnDividerDoubleClick event handler.
仅在特定时间调整大小以适合单元格内容, 以避免性能下降或启用用户调整大小。Adjust sizes to fit cell contents only at specific times to avoid performance penalties or to enable user resizing. 在事件处理程序中调用基于内容的大小调整方法。Call a content-based resizing method in an event handler. 例如, 使用DataBindingComplete事件在绑定后初始化大小, 并CellValidated处理或CellValueChanged事件以调整大小以对绑定数据源中的用户编辑或更改进行补偿。For example, use the DataBindingComplete event to initialize sizes after binding, and handle the CellValidated or CellValueChanged event to adjust sizes to compensate for user edits or changes in a bound data source.
调整多行单元格内容的行高。Adjust row heights for multiline cell contents. 确保列宽适用于显示文本段落, 并使用自动或基于内容的编程行大小调整来调整高度。Ensure that column widths are appropriate for displaying paragraphs of text and use automatic or programmatic content-based row sizing to adjust the heights. 还应确保使用WrapMode单元样式True值显示具有多行内容的单元格。Also ensure that cells with multiline content are displayed using a WrapMode cell style value of True.

通常, 您将使用自动列大小调整模式来维护列宽, 或在调整行高之前将其设置为特定宽度。Typically, you will use an automatic column sizing mode to maintain column widths or set them to specific widths before row heights are adjusted.

用鼠标调整大小Resizing with the Mouse

默认情况下, 用户可以根据单元格值调整不使用自动调整大小模式的行、列和标头的大小。By default, users can resize rows, columns, and headers that do not use an automatic sizing mode based on cell values. 若要防止用户调整其他模式 (如列填充模式) 的大小, 请设置以下DataGridView一个或多个属性:To prevent users from resizing with other modes, such as column fill mode, set one or more of the following DataGridView properties:

还可以通过设置各个行或列的属性, 防止用户调整Resizable它们的大小。You can also prevent users from resizing individual rows or columns by setting their Resizable properties. 默认情况下, Resizable属性值基于列的AllowUserToResizeColumns属性AllowUserToResizeRows值和行的属性值。By default, the Resizable property value is based on the AllowUserToResizeColumns property value for columns and the AllowUserToResizeRows property value for rows. 但是, 如果将Resizable显式True设置False为或, 则指定的值将覆盖该行或列的控件值。If you explicitly set Resizable to True or False, however, the specified value overrides the control value is for that row or column. 设置ResizableNotSet以还原继承。Set Resizable to NotSet to restore the inheritance.

由于NotSet还原值继承Resizable , 因此NotSet属性永远不会返回值, 除非行DataGridView或列尚未添加到控件中。Because NotSet restores the value inheritance, the Resizable property will never return a NotSet value unless the row or column has not been added to a DataGridView control. 如果需要确定是否继承行或Resizable列的属性值, 请检查其State属性。If you need to determine whether the Resizable property value of a row or column is inherited, examine its State property. 如果值包括标志, 则不会Resizable继承属性值。 ResizableSet StateIf the State value includes the ResizableSet flag, the Resizable property value is not inherited.

自动调整大小Automatic Sizing

DataGridView控件中有两种自动调整大小的类型: 列填充模式和基于内容的自动调整大小。There are two kinds of automatic sizing in the DataGridView control: column fill mode and content-based automatic sizing.

列填充模式使控件中的可见列填充控件的显示区域的宽度。Column fill mode causes the visible columns in the control to fill the width of the control's display area. 有关此模式的详细信息, 请参阅Windows 窗体 DataGridView 控件中的列填充模式For more information about this mode, see Column Fill Mode in the Windows Forms DataGridView Control.

您还可以配置行、列和标头以自动调整其大小以适应其单元格内容。You can also configure rows, columns, and headers to automatically adjust their sizes to fit their cell contents. 在这种情况下, 只要单元格内容发生更改, 就会发生大小调整。In this case, size adjustment occurs whenever cell contents change.

备注

如果使用虚拟模式在自定义数据缓存中维护单元值, 则当用户编辑单元值时, 将自动调整大小, 但当你在CellValuePushed事件处理程序外部更改缓存的值时, 将不会发生这种情况。If you maintain cell values in a custom data cache using virtual mode, automatic sizing occurs when the user edits a cell value but does not occur when you alter a cached value outside of a CellValuePushed event handler. 在这种情况下, UpdateCellValue请调用方法来强制控件更新单元格的显示, 并应用当前的自动调整大小模式。In this case, call the UpdateCellValue method to force the control to update the cell display and apply the current automatic sizing modes.

如果对一个维度仅启用了基于内容的自动调整大小 (即, 对于行而不是列) 或列 (而不是行WrapMode ), 则还会启用大小调整, 只要其他维度发生变化。If content-based automatic sizing is enabled for one dimension only—that is, for rows but not columns, or for columns but not rows—and WrapMode is also enabled, size adjustment also occurs whenever the other dimension changes. 例如, 如果将行而不是列配置为自动调整大小WrapMode并启用, 则用户可以拖动列分隔线来更改列的宽度, 行高会自动调整, 以便仍完全显示单元格内容。For example, if rows but not columns are configured for automatic sizing and WrapMode is enabled, users can drag column dividers to change the width of a column and row heights will automatically adjust so that cell contents are still fully displayed.

如果将行和列配置为基于内容的自动调整大小并WrapMode启用DataGridView , 则当单元格内容发生更改时, 控件将调整大小, 并将在计算新大小时使用理想的单元格高度与宽度的比率。If you configure both rows and columns for content-based automatic sizing and WrapMode is enabled, the DataGridView control will adjust sizes whenever cell contents changed and will use an ideal cell height-to-width ratio when calculating new sizes.

若要为标头和行以及不重写控件值的列配置大小调整模式, 请设置以下DataGridView一个或多个属性:To configure the sizing mode for headers and rows and for columns that do not override the control value, set one or more of the following DataGridView properties:

若要为个别列覆盖控件的列大小调整模式, 请将AutoSizeMode其属性设置为以外的NotSet值。To override the control's column sizing mode for an individual column, set its AutoSizeMode property to a value other than NotSet. 列的大小调整模式实际上由其InheritedAutoSizeMode属性确定。The sizing mode for a column is actually determined by its InheritedAutoSizeMode property. 此属性的值基于列的AutoSizeMode属性值, 除非该值为NotSet, 在这种情况下, 将继承控件的AutoSizeColumnsMode值。The value of this property is based on the column's AutoSizeMode property value unless that value is NotSet, in which case the control's AutoSizeColumnsMode value is inherited.

使用基于内容的自动调整大小, 在处理大量数据时务必小心。Use content-based automatic resizing with caution when working with large amounts of data. 若要避免性能损失, 请使用自动大小调整模式, 这些模式仅基于显示的行计算大小, 而不是分析控件中的每一行。To avoid performance penalties, use the automatic sizing modes that calculate sizes based only on the displayed rows rather than analyzing every row in the control. 为了获得最佳性能, 请改用编程大小调整, 以便在特定时间 (例如, 在加载新数据后立即) 调整大小。For maximum performance, use programmatic resizing instead so that you can resize at specific times, such as immediately after new data is loaded.

基于内容的自动调整大小模式不会影响通过Visible将行或列RowHeadersVisible ColumnHeadersVisible或属性设置为false而隐藏的行、列或标头。Content-based automatic sizing modes do not affect rows, columns, or headers that you have hidden by setting the row or column Visible property or the control RowHeadersVisible or ColumnHeadersVisible properties to false. 例如, 如果某个列在自动调整大小以容纳大单元值之后隐藏, 则隐藏列将不会更改其大小 (如果删除包含大单元值的行)。For example, if a column is hidden after it is automatically sized to fit a large cell value, the hidden column will not change its size if the row containing the large cell value is deleted. 当可见性更改时不会进行自动调整大小, 因此Visible , true将列属性更改回后, 不会强制它根据其当前内容重新计算其大小。Automatic sizing does not occur when visibility changes, so changing the column Visible property back to true will not force it to recalculate its size based on its current contents.

基于编程内容的大小调整会影响行、列和标头, 而不考虑它们的可见性。Programmatic content-based resizing affects rows, columns, and headers regardless of their visibility.

以编程方式调整大小Programmatic Resizing

禁用自动调整大小后, 可以通过以下属性以编程方式设置行、列或标题的精确宽度或高度:When automatic sizing is disabled, you can programmatically set the exact width or height of rows, columns, or headers through the following properties:

你还可以使用下列方法以编程方式调整行、列和标头的大小以适应其内容:You can also programmatically resize rows, columns, and headers to fit their contents using the following methods:

这些方法一次调整行、列或标头的大小, 而不是将其配置为连续调整大小。These methods will resize rows, columns, or headers once rather than configuring them for continuous resizing. 新大小将自动计算, 以显示所有单元格内容而不进行剪辑。The new sizes are automatically calculated to display all cell contents without clipping. 但是, 当你以编程方式InheritedAutoSizeMode调整具有属性Fill值的列的大小时, 将使用基于内容的计算宽度按比例FillWeight调整列属性值, 并且实际列宽为然后根据这些新的比例计算, 以便所有列都填充控件的可用显示区域。When you programmatically resize columns that have InheritedAutoSizeMode property values of Fill, however, the calculated content-based widths are used to proportionally adjust the column FillWeight property values, and the actually column widths are then calculated according to these new proportions so that all columns fill the available display area of the control.

以编程方式调整大小有助于避免连续调整大小时的性能下降。Programmatic resizing is useful to avoid performance penalties with continuous resizing. 它还可为用户可调整大小的行、列和标头以及列填充模式提供初始大小。It is also useful to provide initial sizes for user-resizable rows, columns, and headers, and for column fill mode.

通常会在特定时间调用编程大小调整方法。You will typically call the programmatic resizing methods at specific times. 例如, 可以在加载数据后立即以编程方式调整所有列的大小, 也可以在修改特定单元格值后以编程方式调整特定行的大小。For example, you might programmatically resize all columns immediately after loading data, or you might programmatically resize a specific row after a particular cell value has been modified.

自定义基于内容的大小调整行为Customizing Content-based Sizing Behavior

可以在使用派生DataGridViewDataGridViewCell.GetPreferredSize单元格、行和列类型时, 通过重写、 DataGridViewRow.GetPreferredHeightDataGridViewColumn.GetPreferredWidth方法或通过调用派生DataGridView控件.You can customize sizing behaviors when working with derived DataGridView cell, row, and column types by overriding the DataGridViewCell.GetPreferredSize, DataGridViewRow.GetPreferredHeight, or DataGridViewColumn.GetPreferredWidth methods or by calling protected resizing method overloads in a derived DataGridView control. 受保护的调整大小方法重载设计为成对工作以实现理想的单元格高度与宽度的比率, 以避免过于宽或高度的单元格。The protected resizing method overloads are designed to work in pairs to achieve an ideal cell height-to-width ratio, avoiding overly wide or tall cells. AutoResizeRows(DataGridViewAutoSizeRowsMode,Boolean)例如, 如果调用AutoResizeRows方法的重载并falseBoolean参数传入值, 则该重载将计算行中单元格的理想高度和宽度, 但会调整行高仅供参考.For example, if you call the AutoResizeRows(DataGridViewAutoSizeRowsMode,Boolean) overload of the AutoResizeRows method and pass in a value of false for the Boolean parameter, the overload will calculate the ideal heights and widths for cells in the row, but it will adjust the row heights only. 然后, 必须调用AutoResizeColumns方法, 将列宽调整为计算得出的理想。You must then call the AutoResizeColumns method to adjust the column widths to the calculated ideal.

基于内容的大小调整选项Content-based Sizing Options

对于基于内容的大小调整, 大小调整属性和方法使用的枚举具有相似的值。The enumerations used by sizing properties and methods have similar values for content-based sizing. 使用这些值可以限制用于计算首选大小的单元格。With these values, you can limit which cells are used to calculate the preferred sizes. 对于所有大小调整枚举, 名称引用显示单元的值将其计算限制为显示的行中的单元格。For all sizing enumerations, values with names that refer to displayed cells limit their calculations to cells in displayed rows. 在处理大量行时, 排除行有助于避免性能下降。Excluding rows is useful to avoid a performance penalty when you are working with a large quantity of rows. 还可以将计算限制为标头或 nonheader 单元中的单元值。You can also restrict calculations to cell values in header or nonheader cells.

请参阅See also