DataGridView автоматически изменяет размеры отображаемых ячеек для всех столбцов, а затем заполняет до размера таблицы данных - PullRequest
0 голосов
/ 30 мая 2018

Я хотел бы, чтобы размер моих столбцов (сгенерированных из источника данных) соответствовал всем ячейкам, а затем заполнял остальную часть 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

1 Ответ

0 голосов
/ 30 мая 2018

Не устанавливайте более одного столбца в DataGridViewAutoSizeColumnMode.Fill, это вызывает проблемы, потому что все столбцы с заполнением хотят занять оставшуюся часть пространства одновременно.(У меня были проблемы с этим в прошлом.)

Установите все столбцы в DataGridViewAutoSizeColumnMode.DisplayedCells (или другой режим без заполнения) и установите только один из них в DataGridViewAutoSizeColumnMode.Fill так, чтобычто один будет использовать оставшуюся часть пространства.

DataGridViewAutoSizeColumnMode Enumeration

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...