[RESOLVED] Resizing DGV rows so all records fit without scrolling?
Hi guys,
I have a student attendants system with SQL, I want to put tablets in the classrooms with the attendants so the students can see
I have a form with a DGV with 3 columns (Name, Time scanned and Pic(The Pic is a red, orange or green circle))
The form is maximized on the screen, the DGV is maximized on the form.
How do I get the rows on the DGV to auto resize and fill the DGV with ALL the records showing without scrolling?
Thanks
Re: Resizing DGV rows so all records fit without scrolling?
As there is no "Fill" for rows, it is most likely you will have to iterate the rows and manually set the row height of each row to that of the dgv.height / count of rows. you will neeed Cint to convert this value into a whole number
Re: Resizing DGV rows so all records fit without scrolling?
You will need to set the ScrollBars property of the DataGridView to only display the vertical scroll-bar, but take a look at this code:
Code:
Private Sub DataGridView1_RowSizingChanged(sender As Object, e As EventArgs) Handles DataGridView1.RowsAdded, DataGridView1.RowsRemoved, Me.SizeChanged
'Get the total height of the displayed area of the DataGridView
Dim visible_height As Integer = DataGridView1.Height - If(DataGridView1.ColumnHeadersVisible, DataGridView1.ColumnHeadersHeight, 0)
'Divide the number of rows by the height
Dim row_height As Integer = visible_height \ DataGridView1.RowCount
'Set the height of each row
For Each r As DataGridViewRow In DataGridView1.Rows
r.Height = row_height
Next
End Sub
1 Attachment(s)
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
dday9
You will need to set the ScrollBars property of the DataGridView to only display the vertical scroll-bar, but take a look at this code:
Code:
Private Sub DataGridView1_RowSizingChanged(sender As Object, e As EventArgs) Handles DataGridView1.RowsAdded, DataGridView1.RowsRemoved, Me.SizeChanged
'Get the total height of the displayed area of the DataGridView
Dim visible_height As Integer = DataGridView1.Height - If(DataGridView1.ColumnHeadersVisible, DataGridView1.ColumnHeadersHeight, 0)
'Divide the number of rows by the height
Dim row_height As Integer = visible_height \ DataGridView1.RowCount
'Set the height of each row
For Each r As DataGridViewRow In DataGridView1.Rows
r.Height = row_height
Next
End Sub
Thanks for the reply,
I pasted the code into my program, and I got an error "System.DivideByZeroException: 'Attempted to divide by zero.'"
Attachment 156539
Re: Resizing DGV rows so all records fit without scrolling?
Code:
With DataGridView1
Dim DGVHeight As Integer = CInt(.Height - .ColumnHeadersHeight)
For Each DGVRow As DataGridViewRow In .Rows
DGVRow.Height = CInt(DGVHeight / .RowCount)
Next
End With
Re: Resizing DGV rows so all records fit without scrolling?
Which variable is 0: visible_height or the RowCount?
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
kpmc
Code:
With DataGridView1
Dim DGVHeight As Integer = CInt(.Height - .ColumnHeadersHeight)
For Each DGVRow As DataGridViewRow In .Rows
DGVRow.Height = CInt(DGVHeight / .RowCount)
Next
End With
Thanks, this worked!!
Now I need the Columns to fit to the entire DGV
(but I can't have "AutoSizeColumnsMode = Fill" because the "Name" column needs to be long, the "Scan Time" and the "Pic" columns needs to be short)
Re: Resizing DGV rows so all records fit without scrolling?
Many ways to get there, here's one
Code:
With DataGridView1
Dim DGVHeight As Integer = CInt(DataGridView1.Height - DataGridView1.ColumnHeadersHeight)
'rows
For Each DGVRow As DataGridViewRow In .Rows
DGVRow.Height = CInt(DGVHeight / .RowCount)
Next
.Columns("Col1").Width = 200 'insert your desired width
For Each DGVCol As DataGridViewColumn In .Columns
If Not DGVCol.Name = "Col1" Then
DGVCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End If
Next
End With
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
kpmc
Many ways to get there, here's one
Code:
With DataGridView1
Dim DGVHeight As Integer = CInt(DataGridView1.Height - DataGridView1.ColumnHeadersHeight)
'rows
For Each DGVRow As DataGridViewRow In .Rows
DGVRow.Height = CInt(DGVHeight / .RowCount)
Next
.Columns("Col1").Width = 200 'insert your desired width
For Each DGVCol As DataGridViewColumn In .Columns
If Not DGVCol.Name = "Col1" Then
DGVCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End If
Next
End With
Thanks, worked perfectly!
Just 1 thing, it still cuts a little from the top and bottom of the picture
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
threeeye
Thanks, worked perfectly!
Just 1 thing, it still cuts a little from the top and bottom of the picture
Put something like this before the rows loops, not real sure it will work as is, you may have to add "MyImgCol" to the collection and remove the org
Code:
Dim MyImgCol = DirectCast(.Columns("yourimage"), DataGridViewImageColumn)
MyImgCol.ImageLayout = DataGridViewImageCellLayout.Zoom
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
kpmc
Put something like this before the rows loops, not real sure it will work as is, you may have to add "MyImgCol" to the collection and remove the org
Code:
Dim MyImgCol = DirectCast(.Columns("yourimage"), DataGridViewImageColumn)
MyImgCol.ImageLayout = DataGridViewImageCellLayout.Zoom
Works perfectly!!!
Thanks!!!
I think I have all my answers answered...
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
threeeye
Works perfectly!!!
Thanks!!!
I think I have all my answers answered...
That worked without having to add "MyImgCol" to the column collection?
Re: Resizing DGV rows so all records fit without scrolling?
Quote:
Originally Posted by
kpmc
That worked without having to add "MyImgCol" to the column collection?
This is my DGVSizeing code:
Code:
With DataGridView1
Dim MyImgCol = DirectCast(.Columns("Pic"), DataGridViewImageColumn)
MyImgCol.ImageLayout = DataGridViewImageCellLayout.Zoom
Dim DGVHeight As Integer = CInt(DataGridView1.Height - DataGridView1.ColumnHeadersHeight)
'rows
For Each DGVRow As DataGridViewRow In .Rows
DGVRow.Height = CInt(DGVHeight / .RowCount)
Next
.Columns("Name").Width = 500
.Columns("Time").Width = 110
.Columns("Pic").Width = 110
End With