DatagridView:删除未使用的空间?

问题描述 投票:0回答:10

我想知道是否可以删除C#中

DataGridView
控件的未使用空间(灰色空间)。我必须让
DataGridView
仅显示白色表格。

有什么建议吗?

注意:这篇文章最初包含不再有效的外部图像

c# datagridview space
10个回答
10
投票

有时(尤其是 winforms)最好的方法是破解:

dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control;

我从这个帖子里偷来的: 在c#中删除数据网格中的空白灰色空间


6
投票

我发现没有简单的方法来删除“未使用”或灰色(背景颜色)空间。然而,对我来说一个有效的解决方案是隐藏 DataGridView 的边框并将其背景颜色更改为周围控件的背景。本质上,这种感觉是不再有未使用的空间。

这是伪代码片段:

TableGridView = DataGridView()
TableGridView.Width = 0
TableGridView.Height = 0
TableGridView.AutoSize = true 
TableGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
TableGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
TableGridView.BackgroundColor = SystemColors.ControlLightLight
TableGridView.BorderStyle = BorderStyle.None

我在某处读到 AutoSize 设置不适用,但是,它确实改变了我的情况。此示例表明周围控件的背景颜色为 SystemColors.ControlLightLight,但这可以根据需要进行修改。

如果对您有帮助,请投票。


5
投票

RowsHeaderVisible
属性设置为 false,您可以从设计器的类别
Appearence
中执行此操作,也可以从代码中执行此操作:

dataGridView1.RowsHeaderVisible = false;

为了去掉左边的指示行,至于剩下的灰色空间,可以尝试将前面提到的

AutoSizeColumnsMode
设置为填充,但仍然会出现下部因缺少行而变灰的情况。

您可以调整网格大小以适合单元格,而不是调整单元格大小以填充网格。这是否是可接受的方法将取决于您的意图。

我的意思是,如果只是颜色困扰您,将背景色设置为白色可能会解决问题。


3
投票

我相信你想要:

myDataGrid.AutoSizeColumnsMode = Fill

编辑:这只是调整列的大小。我不确定除了调整网格高度之外如何消除行灰色空间。


1
投票
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

0
投票

嗯,我之前费尽心思找到了这个问题的答案,但最后如果你想模仿一个空的 DataGridView 那么长的答案是创建“White”矩形对象并使用 Graphics 在重写的 OnPaint 上填充整个网格方法。


0
投票

去找设计师:

1)将datagridview背景颜色更改为与表单颜色相同

2)将datagridview“BorderStyle”设置为None


0
投票

您必须设置固定的行高,然后使 DGV 的高度精确为行高的倍数,再加上几个像素。

    DGV1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None                                  
    DGV1.RowTemplate.Height = 25                                                               
    DGV1.Height = 252

然后,您将始终显示完整的行数,以及底部的小边距,该边距不会是灰色的。要发现显示了哪些行,请使用:

    Dim firstVisibleRowIndex As Integer = DGV1.FirstDisplayedCell.RowIndex
    Dim lastVisibleRowIndex As Integer = firstVisibleRowIndex + DGV1.DisplayedRowCount(False) - 1

0
投票

这个问题很老了,但仍然存在,所以即使过了这么久,另一个解决方法可能会被证明是有用的。

最近我遇到了同样的问题,并且没有找到完美的解决方案,但在我的情况下,我最终想出的解决方案与我发现的相比是最不烦人的。

由于自动大小本质上是有问题的,唯一的方法似乎是使用事件重新实现。

问题是关于C#的,我只有一个PowerShell示例,因为这是我遇到这个问题时使用的,但根本原因是在WinForms中,Powershell使用相同的东西,通过稍微不同的API。 这意味着,下面的示例显示了也可以使用 C# 完成的解决方法的主要步骤。

# Import
Add-Type -assembly System.Windows.Forms

# Initialize main form
$window = New-Object System.Windows.Forms.Form
$window.Text ='Test App'

# This is for the window to follow the content
# Although this is technically not relevant, it can limit the options for the workaround because this auto size is not always able to follow the weirdness that's going on
$window.AutoSize = $true

# Create columns
$dataTable = New-Object System.Data.DataTable
$dataTable.Columns.Add("Col1", [System.Type]::GetType("System.Int32"))
$dataTable.Columns.Add("Col2", [System.Type]::GetType("System.Int32"))

# Initialize DataGridView
$grid = New-Object System.Windows.Forms.DataGridView
$grid.DataSource = $dataTable

# These can be anything, but will affect what the sizing needs to be exactly
$grid.ColumnHeadersVisible = $true
$grid.RowHeadersVisible = $true
$grid.BorderStyle = 0

# These are not required, the point is that it works even if the user is poking around
$grid.ReadOnly = $false
$grid.AllowUserToAddRows = $true
$grid.AllowUserToDeleteRows = $true
$grid.AllowUserToOrderColumns = $true
$grid.AllowUserToResizeColumns = $true
$grid.AllowUserToResizeRows = $true

$resizeLogic = {
    # The height and width of the actual grid are available so that the host can be resized to the same size. Note, that this is affected by having the header and any borders
    # See the doc for the magic zeroes: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewelementstates?view=windowsdesktop-8.0
    $grid.Height = $grid.Rows.GetRowsHeight(0) + $grid.ColumnHeadersHeight
    $grid.Width = $grid.Columns.GetColumnsWidth(0) + $grid.RowHeadersWidth
    # The resize above seems to somehow dodge the main frame's auto-sizing, so this is a gentle reminder.
    # Note, that this limits the Events that can be used as resizing the window triggers some rendering-related events of the grid view
    $window.Height = 0
    $window.Width = 0
}

# Register all events that can impact the size
$grid.Add_RowHeightChanged($resizeLogic)
$grid.Add_ColumnWidthChanged($resizeLogic)
$grid.Add_RowsAdded($resizeLogic)
$grid.Add_RowsRemoved($resizeLogic)
$grid.Add_ColumnAdded($resizeLogic)
$grid.Add_ColumnRemoved($resizeLogic)

# Just some dummy rows
$dataTable.Rows.Add(1)
$dataTable.Rows.Add(2)

# Assemble and show the window
$window.Controls.Add($grid)
$window.ShowDialog()

请注意,这是一个情境解决方案,但与使网格视图背景“不可见”相比,我更喜欢它,因为这样就没有随机死区,即使它不那么明显。

但这有两个缺点:

  • 调整大小“闪烁”一次(这意味着内容至少对我来说变黑约 1 帧)
  • 最后一行和最后一列的大小无法增加,因为光标只能在网格视图宿主区域内移动(这可能是未使用区域存在的原因,并且可能可以解决,但这对我来说已经足够了)

-2
投票

我使用此代码,如果您不添加按钮列或图像,它适用于我,我从网站上获取它,但我不记得从哪里:

For Each row As DataGridViewRow In DataGridView1.Rows
If datagrid_limits > newHeight Then
newHeight =newHeight + 40
Else
Exit For
End If
Next
DataGridView1.Height = newHeight
© www.soinside.com 2019 - 2024. All rights reserved.