DataGridViewAutoSizeColumnsMode 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义用于指定如何调整列宽的值。
public enum class DataGridViewAutoSizeColumnsMode
public enum DataGridViewAutoSizeColumnsMode
type DataGridViewAutoSizeColumnsMode =
Public Enum DataGridViewAutoSizeColumnsMode
- 继承
字段
AllCells | 6 | 列宽调整到适合列中所有单元格(包括标头单元格)的内容。 |
AllCellsExceptHeader | 4 | 列宽调整到适合列中除标头单元格以外所有单元格的内容。 |
ColumnHeader | 2 | 列宽调整到适合列标头单元格的内容。 |
DisplayedCells | 10 | 列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(包括标头单元格)的内容。 |
DisplayedCellsExceptHeader | 8 | 列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(不包括标头单元格)的内容。 |
Fill | 16 | 列宽调整到使所有列宽精确填充控件的显示区域,要求使用水平滚动的目的只是保持列宽大于 MinimumWidth 属性值。 相对列宽由相对 FillWeight 属性值决定。 |
None | 1 | 列宽不会自动调整。 |
示例
下面的代码示例演示了在主/细节方案中使用此枚举,其中两 DataGridView 个控件在父/子关系中显示来自两个表的数据。 在此示例中,主控件的列大小调整模式为 None,并且以编程方式初始化列宽以适应加载的值。 详细信息控件设置为自动大小调整模式,以便每当值更改 (时,列都会自动调整,例如,当用户更改父表中的当前行时) 。 此示例是 How to: Create a Master/Detail Form Using Two Windows 窗体 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 可以调整其列的大小,使其填充控件的可用水平宽度,或使它们显示所有单元格或指定单元格的完整内容。
使用 Fill 模式时,列的首选宽度是通过调整该模式下所有列的大小来确定的,以便控件中的所有可见列完全填充可用显示区域的水平宽度。 与其他模式一样,首选列宽是显示该列中所有单元格或单元格子集的最大单元格值所需的最小宽度,例如当前显示的行中的单元格子集。 使用单元格子集来确定新宽度对于避免使用多行数据时的性能损失非常有用。
基于内容的自动调整大小可防止用户使用鼠标调整列宽。 但是,用户调整大小在填充模式下处于启用状态。
控件属性和AutoResizeColumns方法使用此DataGridViewAutoSizeColumnsMode枚举。
有关大小调整模式的详细信息,请参阅 Windows 窗体 DataGridView 控件中的大小调整选项。 有关列填充模式的详细信息,请参阅 Windows 窗体 DataGridView 控件中的列填充模式。