【问题标题】:DataGridView autosizemode to displayed cells for all columns and then fill to the datagridview sizeDataGridView autosizemode 为所有列显示单元格,然后填充到 datagridview 大小
【发布时间】:2018-05-30 15:12:53
【问题描述】:

我想让我的列(从数据源生成)大小适合所有单元格,然后填充 DataGridView 的其余部分。我正在使用 DataBindingComplete 来自动调整列大小,因此在这方面应该没有任何问题,但是在四处搜索之后我还没有找到适合的方法,然后在需要时填充。任何帮助将不胜感激!根据要求,autosizemode 的代码如下。

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

【问题讨论】:

  • 始终显示您的代码。
  • 我可以这样做,但到目前为止,所有列都只是填充到 datagridview 的大小
  • 听起来它正在工作。
  • 我的目标是让列首先调整大小以适应其整个单元格,然后用剩余的空白填充 datagridview,而不是统一调整大小以适应 datagridview。
  • 我希望列首先使所有单元格适合它们,然后均匀增长以适应datagridview的大小。到目前为止,它们都只是增长以适应 datagridview。这会导致一些较长的列无法完全显示,而短的列有额外的空间

标签: .net vb.net winforms visual-studio-2017


【解决方案1】:

不要为 DataGridViewAutoSizeColumnMode.Fill 设置多于一列,这会导致问题,因为所有具有 Fill 的列都希望同时占用其余空间。 (我过去曾遇到过这样的问题。)

将所有列设置为 DataGridViewAutoSizeColumnMode.DisplayedCells(或其他非填充模式),并将其中一列设置为 DataGridViewAutoSizeColumnMode.Fill,以便该列使用其余空间。

DataGridViewAutoSizeColumnMode 枚举

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多