Solved Datagrid row height and scrolling problem!

Posted on 2006-05-18
Last Modified: 2012-06-21
I have a winforms application using a Datagrid which i have bound to a dataview.
I have to display quite alot of data in each row and therefore i have used the code below to set the row heights, which is a VB version of the C# code on syncfusion.  
The rowheights will set to the correct value ok, but if the bottom rows are expanded, the scroll bar wont allow me to scroll down to the bottom of the datagrid, and therefore you can not view them.  However if i select a row i can see and scroll down using the mouse wheel they come into view.  I have tried allsorts to try and refresh the grid so the scrollbar recognises the size but so far no luck.
Any ideas would be great...

The class i have used to expand the rows is below, followed by the code i have used in my main form to call it.

Imports System
Imports System.Reflection

Public Class DataGridRowHeightSetter
    Private dg As DataGrid
    Private rowObjects As ArrayList

    Public Sub New(ByVal dg As DataGrid)
        Me.dg = dg
    End Sub 'New

    Private Sub InitHeights()
        ' get the original type so that method info works.
        Dim t As Type = mainform.dataGridMain.GetType.BaseType
        Dim mi As MethodInfo = t.GetMethod("get_DataGridRows", BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static)
        Dim dgra As System.Array = CType(mi.Invoke(Me.dg, Nothing), System.Array)
        rowObjects = New ArrayList
        Dim dgrr As Object
        For Each dgrr In dgra
            If dgrr.ToString().EndsWith("DataGridRelationshipRow") = True Then
            End If
        Next dgrr
    End Sub 'InitHeights

    Default Public Property Item(ByVal row As Integer) As Integer
                Dim pi As PropertyInfo = rowObjects(row).GetType().GetProperty("Height")
                Return Fix(pi.GetValue(rowObjects(row), Nothing))
                Throw New ArgumentException("invalid row index")
            End Try
        End Get
        Set(ByVal Value As Integer)
                Dim pi As PropertyInfo = rowObjects(row).GetType().GetProperty("Height")
                pi.SetValue(rowObjects(row), Value, Nothing)
                Throw New ArgumentException("invalid row index")
            End Try
        End Set
    End Property
End Class 'DataGridRowHeightSetter

    Private Sub expandRows()
            'create an instance of the DataGridRowHeightSetter class
            rowHeights = New DataGridRowHeightSetter(Me.dataGridMain)
            Dim rowCount As Integer = mdt_datatable.Rows.Count ' get number of rows in grid/datatable
            Dim Lines As Integer
            For i As Integer = 0 To rowCount - 1
                Lines = (CType(Me.dataGridMain.Item(i, 16), Integer) * (Me.dataGridMain.Font.Height + 3))
                    rowHeights(i) = Lines
                End If
            btnCollapse.Text = "Collapse"
            expanded = True
        Catch ex As Exception
        End Try
    End Sub
Question by:Andy_Webster
    LVL 2

    Author Comment

    hmm ive found that this kind of works! but it isnt the best solution...

    Dim vSB As VScrollBar = Nothing
            For Each c As Control In Me.dataGridMain.Controls
                If TypeOf c Is VScrollBar Then
                    vSB = CType(c, VScrollBar)
                End If
    vSB.Maximum += 10%

    Accepted Solution

    Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now