Home > database >  Datagridview width/height to eliminate grey area
Datagridview width/height to eliminate grey area

Time:11-09

I'm trying to present some data in datagridview on a form dynamically (programmatically adding DGV). I've tried almost every combination of properties, but I cannot eliminate grey areas on the right, and on the bottom. enter image description here

Here are my properties that are set at form load

With dgv
.DataSource = dTable
.Dock = DockStyle.None
.AllowUserToAddRows = False
.AllowUserToOrderColumns = False
.AllowUserToDeleteRows = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.EditMode = DataGridViewEditMode.EditProgrammatically
.RowHeadersVisible = False
.DefaultCellStyle.WrapMode = False
.ColumnHeadersVisible = True
.SelectionMode = DataGridViewSelectionMode.CellSelect
.MultiSelect = True
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.BorderStyle = BorderStyle.None
.ColumnHeadersDefaultCellStyle.Font = New Font("Segoe UI", 8)
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
width = 0
For i As Integer = 0 To .ColumnCount - 1
  .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
  .Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
  .Columns(i).DefaultCellStyle.Font = New Font("Segoe UI", 8)
  width  = .Columns(i).Width
Next i
.AutoResizeColumns()
.AutoResizeRows()
Dim height As Integer = .ColumnHeadersHeight
For i As Integer = 0 To .Rows.Count - 1
  height  = .Rows(i).Height
  .Rows(i).Resizable = False
Next i
.AutoSize = True
.Width = width
.Height = height - 8
end width

Any suggestions how I can solve the problem?

CodePudding user response:

O.K so what I used is to size the datagridview with: DataGridView1.Width = colwidth DataGridView1.RowHeadersWidth

CodePudding user response:

As I commented, I do not think the DataGridView has a “built-in” mechanism to auto size the rows to “fill” the grid height. I could be mistaken, but, I believe you will need to set the row height row by row. All the examples I saw failed in my tests. Obviously I may have done something wrong, however, I felt it should not be too difficult to crudely “calculate” the row heights after the grid is filled with data.

For grids where the total number of rows are always less than the grids height then the code below should work to fill the grids height and remove the bottom “grey” area.

If the grid has many rows that already fill or go beyond the grids height then the code below will resize the rows such that the rows should fit (or close to fit) evenly in the grid row area. In other words, it shouldn’t have the last row “chopped” off in the middle of the row and it should fit closely to the bottom of the grid.

To get the row height may look something like…

Dim rowHeightDouble As Double = (dgv.Height - dgv.ColumnHeadersHeight) / dgv.DisplayedRowCount(False)
Dim rowHeight As Int32 = CInt(Int(rowHeightDouble))
For Each row As DataGridViewRow In dgv.Rows
  row.Height = rowHeight
  row.MinimumHeight = rowHeight
Next

rowHeight is calculated by taking the height of the grid minus the column header height and divide that number by the number of “displayed” rows in the grid. Then we loop through all the rows and set the height for each row.

Note: Obviously, this division to get the rowHeight from the number of displayed rows may not divide evenly into the height of the grid. Example: let’s say the grid’s rows area height is 188, then… if there are 9 displayed rows … then, after the division (188 / 9 = 20.88) means the row height is 20 and the remainder is 8. So this would show 8 pixels of “grey” space below the last row.

Therefore, to get the rows to line up as close to the bottom of the grid as possible without displaying the vertical scroll bar, we could add 8 pixels to the last row in the gird to get rid of the “grey” area. Obviously, you could also add 1 pixel to the first 8 rows, however I took the lazy way out and added it all to the last row. That may look something like…

If (dgv.Rows.Count = dgv.DisplayedRowCount(False)) Then
  Dim lastRowBuff As Int32 = CInt(Int((dgv.Height - dgv.ColumnHeadersHeight) - (rowHeight * dgv.DisplayedRowCount(False))))
  dgv.Rows(dgv.DisplayedRowCount(False) - 1).Height = rowHeight   lastRowBuff
End If

I hope this makes sense and helps. Using your posted code I changed the grids AutoSizeColumnsMode to…

.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

Then I dumped everything after the first for loop and replace it with the code above.

enter image description here

With dgv
  .DataSource = dTable
  .Dock = DockStyle.None
  .AllowUserToAddRows = False
  .AllowUserToOrderColumns = False
  .AllowUserToDeleteRows = False
  .AllowUserToResizeColumns = False
  .AllowUserToResizeRows = False
  .EditMode = DataGridViewEditMode.EditProgrammatically
  .RowHeadersVisible = False
  .DefaultCellStyle.WrapMode = DataGridViewTriState.False
  .ColumnHeadersVisible = True
  .SelectionMode = DataGridViewSelectionMode.CellSelect
  .MultiSelect = True
  '.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
  .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
  .BorderStyle = BorderStyle.None
  .ColumnHeadersDefaultCellStyle.Font = New Font("Segoe UI", 8)
  .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  'Width = 0
  For i As Integer = 0 To .ColumnCount - 1
    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
    .Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
    .Columns(i).DefaultCellStyle.Font = New Font("Segoe UI", 8)
    'Width  = .Columns(i).Width
  Next i
  Dim rowHeightDouble As Double = (dgv.Height - dgv.ColumnHeadersHeight) / dgv.DisplayedRowCount(False)
  Dim rowHeight As Int32 = CInt(Int(rowHeightDouble))
  For Each row As DataGridViewRow In dgv.Rows
    row.Height = rowHeight
    row.MinimumHeight = rowHeight
  Next
  If (dgv.Rows.Count = dgv.DisplayedRowCount(False)) Then
    Dim lastRowBuff As Int32 = CInt(Int((dgv.Height - dgv.ColumnHeadersHeight) - (rowHeight * dgv.DisplayedRowCount(False))))
    dgv.Rows(dgv.DisplayedRowCount(False) - 1).Height = rowHeight   lastRowBuff
  End If
End With
  • Related