我希望我的列(从数据源生成(的大小适合所有单元格,然后填充 DataGridView 的其余部分。我正在使用 DataBindingComplete 来自动调整列的大小,因此该端应该没有任何问题,但是在搜索之后,我还没有找到适合的方法,然后根据需要填充。任何帮助将不胜感激!根据要求,自动调整大小模式的代码如下。
Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(6).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(7).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView1.Columns(8).Visible = False
End Sub
不要将超过一列设置为 DataGridViewAutoSizeColumnMode.Fill,这会导致问题,因为所有具有 Fill 的列都希望同时占用其余空间。(我过去对此有过问题。
将所有列设置为 DataGridViewAutoSizeColumnMode.DisplayCells(或其他非填充模式(,并仅将其中一个列设置为 DataGridViewAutoSizeColumnMode.Fill,以便该列将使用其余空间。
数据网格视图自动调整列模式枚举
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewautosizecolumnmode%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Member Name Description
AllCells The column width adjusts to fit the contents of all cells in the column, including the header cell.
AllCellsExceptHeader The column width adjusts to fit the contents of all cells in the column, excluding the header cell.
ColumnHeader The column width adjusts to fit the contents of the column header cell.
DisplayedCells The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, including the header cell.
DisplayedCellsExceptHeader The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, excluding the header cell.
Fill The column width adjusts so that the widths of all columns exactly fills the display area of the control, requiring horizontal scrolling only to keep column widths above the DataGridViewColumn.MinimumWidth property values. Relative column widths are determined by the relative DataGridViewColumn.FillWeight property values.
None The column width does not automatically adjust.
NotSet The sizing behavior of the column is inherited from the DataGridView.AutoSizeColumnsMode property.