C# WinForm(表單) DataGridView (Rows)列的寬度、行的高度自動調整
C# WinForm(表單) DataGridView (Rows)列的寬度、行的高度自動調整
資料來源: https://www.itread01.com/content/1546641372.html
https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.datagridviewautosizecolumnsmode?redirectedfrom=MSDN&view=windowsdesktop-6.0
https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.datagridviewautosizerowsmode?redirectedfrom=MSDN&view=windowsdesktop-6.0
01.列的寬度自動調整 [DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;]
| AllCells | 6 |
列宽调整到适合列中所有单元格(包括标头单元格)的内容。 |
| AllCellsExceptHeader | 4 |
列宽调整到适合列中除标头单元格以外所有单元格的内容。 |
| ColumnHeader | 2 |
列宽调整到适合列标头单元格的内容。 |
| DisplayedCells | 10 |
列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(包括标头单元格)的内容。 |
| DisplayedCellsExceptHeader | 8 |
列宽调整到适合位于屏幕上当前显示的行中的列的所有单元格(不包括标头单元格)的内容。 |
| Fill | 16 |
列宽调整到使所有列宽精确填充控件的显示区域,要求使用水平滚动的目的只是保持列宽大于 MinimumWidth 属性值。 相对列宽由相对 FillWeight 属性值决定。 |
| None | 1 |
列宽不会自动调整。 |
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;
}
02.列的寬度高度調整 [DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;]
| AllCells | 7 |
将行高调整到适合行中所有单元格(包括标头单元格)的内容。 |
| AllCellsExceptHeaders | 6 |
将行高调整到适合行中所有单元格(不包括标头单元格)的内容。 |
| AllHeaders | 5 |
将行高调整到适合行标头的内容。 |
| DisplayedCells | 11 |
将行高调整到适合屏幕上当前显示的行中所有单元格(包括标头单元格)的内容。 |
| DisplayedCellsExceptHeaders | 10 |
将行高调整到适合屏幕上当前显示的行中所有单元格(不包括标头单元格)的内容。 |
| DisplayedHeaders | 9 |
将行高调整到适合屏幕上当前显示的行标头的内容。 |
| None | 0 |
行高不自动调整。 |
private void AutoSizeRowsMode(Object sender, EventArgs es)
{
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.AllCells;
}