-
Feb 20th, 2018, 01:06 PM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 20th, 2018, 01:59 PM
#2
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
-
Feb 20th, 2018, 01:59 PM
#3
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
-
Feb 21st, 2018, 08:57 AM
#4
Thread Starter
Hyperactive Member
Re: Resizing DGV rows so all records fit without scrolling?
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.'"
-
Feb 21st, 2018, 09:04 AM
#5
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
Last edited by kpmc; Feb 21st, 2018 at 09:08 AM.
-
Feb 21st, 2018, 09:46 AM
#6
Re: Resizing DGV rows so all records fit without scrolling?
Which variable is 0: visible_height or the RowCount?
-
Feb 21st, 2018, 01:03 PM
#7
Thread Starter
Hyperactive Member
Re: Resizing DGV rows so all records fit without scrolling?
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)
-
Feb 21st, 2018, 03:39 PM
#8
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
-
Feb 21st, 2018, 04:26 PM
#9
Thread Starter
Hyperactive Member
Re: Resizing DGV rows so all records fit without scrolling?
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
Last edited by threeeye; Feb 21st, 2018 at 04:29 PM.
-
Feb 22nd, 2018, 09:05 AM
#10
Re: Resizing DGV rows so all records fit without scrolling?
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
-
Feb 23rd, 2018, 09:21 AM
#11
Thread Starter
Hyperactive Member
Re: Resizing DGV rows so all records fit without scrolling?
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...
-
Feb 23rd, 2018, 09:29 AM
#12
Re: Resizing DGV rows so all records fit without scrolling?
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?
-
Feb 26th, 2018, 10:12 AM
#13
Thread Starter
Hyperactive Member
Re: Resizing DGV rows so all records fit without scrolling?
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|