Results 1 to 13 of 13

Thread: [RESOLVED] Resizing DGV rows so all records fit without scrolling?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Resolved [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

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by dday9 View Post
    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.'"
    Name:  DGV.jpg
Views: 358
Size:  16.6 KB

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Resizing DGV rows so all records fit without scrolling?

    Which variable is 0: visible_height or the RowCount?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by kpmc View Post
    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)

  8. #8
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by kpmc View Post
    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.

  10. #10
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by threeeye View Post
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by kpmc View Post
    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...

  12. #12
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by threeeye View Post
    Works perfectly!!!
    Thanks!!!
    I think I have all my answers answered...
    That worked without having to add "MyImgCol" to the column collection?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: Resizing DGV rows so all records fit without scrolling?

    Quote Originally Posted by kpmc View Post
    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
  •  



Click Here to Expand Forum to Full Width