Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Row Sizing

Status
Not open for further replies.

DaedalusPrime

Programmer
May 25, 2005
13
0
0
US
I'm working on a project that makes extensive use of dataGrids to present data to the user for viewing and editing. I have a separate form of preferences where users can specify row height for each individual grid.

I have some code down that will set the height, but it has two problems:
First, if a scroll bar is needed due to expanded row size, it doesn't appear and if there was one there already, it's broken until the user closes and reopens the form on which the datagrid lies.

Second, using this method if the user tries to resize a column to make it very small (i.e. a width of zero or one pixel) an exception is thrown somewhere by System.Windows.Forms.dll, citing a null reference.

Here is the code I am using currently to resize rows:
Code:
Public Class DataGridRowHeightSetter
    Private dg As DataGrid
    Private rowObjects As ArrayList


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


    Private Sub InitHeights()
        Dim mi As MethodInfo = dg.GetType().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
                rowObjects.Add(dgrr)
            End If
        Next dgrr
    End Sub 'InitHeights 

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

I also have a function to make use of the class to change row heights that looks like this
Code:
    Public Sub adjustRowHeight(ByVal dg As System.Windows.Forms.DataGrid, ByVal ds As DataSet, ByVal newRowHeight As Integer)

        'set row heights for all cells on the datagrid
        rowHeights = New DataGridRowHeightSetter(dg)
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count() - 1
            rowHeights(i) = newRowHeight
        Next

    End Sub


Thanks for any help you can give!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top