Tipp 0068 DataGrid-Zeilenhöhen automatisch anpassen
Autor/Einsender:
Datum:
  Klaus D. Raudszus
09.02.2005
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Dieses Beispiel stellt eine Klasse zur Verfügung, die die Höhe der einzelne Zeilen in einem DataGrid automatisch anpasst. Das Beispiel demonstriert, wie mit dem Namespace System.Reflection Methoden und Eigenschaften nicht nur ausgelesen sondern auch gesetzt werden können.
Code in modInitDataGrid
 
Module modInitDataGrid
  Public Sub InitDataGrid(ByVal m_datagrid As DataGrid)
    With m_datagrid
      .ReadOnly = True
      .Size = New Size(384, 300)
      .BackgroundColor = Color.LightSalmon
      .CaptionText = "DataGrid Reihen anklicken"
      .CaptionBackColor = Color.LightSalmon
      .CaptionForeColor = Color.Black
      .CaptionFont = New Font("Arial", 9.0!, _
            FontStyle.Bold Or FontStyle.Italic, _
            GraphicsUnit.Point, CType(0, Byte))
      .Font = New Font("Arial", 9.0!, FontStyle.Bold, _
            GraphicsUnit.Point, CType(0, Byte))
    End With

    Dim newDataGridTableStyle As DataGridTableStyle = _
          New DataGridTableStyle()
    With newDataGridTableStyle
      .MappingName = "element1"
      .HeaderBackColor = Color.Salmon
      .HeaderFont = New Font("Arial", 9.0!, _
            FontStyle.Bold Or FontStyle.Italic, _
            GraphicsUnit.Point, CType(0, Byte))
      .BackColor = Color.LightSalmon
      .AlternatingBackColor = Color.Salmon
    End With

    Dim newTextColumnLand As DataGridTextBoxColumn =
          New DataGridTextBoxColumn()
      With newTextColumnLand
        .MappingName = "Land"
        .HeaderText = "TextBoxColumn"
        .Width = 180
      End With
      newDataGridTableStyle.GridColumnStyles.Add(newTextColumnLand)

    Dim newTextColumnKont As DataGridTextBoxColumn = _
          New DataGridTextBoxColumn()
    With newTextColumnKont
      .MappingName = "Kont"
      .HeaderText = "TextBoxColumn"
      .Width = 150
    End With
    newDataGridTableStyle.GridColumnStyles.Add(newTextColumnKont)
    m_datagrid.TableStyles.Add(newDataGridTableStyle)
    Call FillDataGrid(m_datagrid)
  End Sub

  Private Sub FillDataGrid(ByVal mDatagrid As DataGrid)
    Dim Dataset As newDataset = New newDataset()
    mDatagrid.SetDataBinding(Dataset.element1, Nothing)
    With Dataset.element1()
      .Addelement1Row(DText, "Europa")
      .Addelement1Row(FText, "Europa")
      .Addelement1Row(KText, "Amerika")
      .Addelement1Row(JText, "Asien")
      .Addelement1Row("Spanien", "Europa")
      .Addelement1Row("United Kingdom", "Europa")
      .Addelement1Row("Vereinigte Staaten", "Amerika")
    End With
    mDatagrid.Select()
    Dataset.Dispose()
  End Sub

  Private Function DText() As String
    Return "Deutschland" & Chr(13) & Chr(10) & _
        "Einwohnerzahl = 82 Mio" & Chr(13) & Chr(10) & _
        "Fläche = 357.022 Km²"
  End Function

  Private Function FText() As String
    Return "Frankreich" & Chr(13) & Chr(10) & _
        "Einwohnerzahl = 58,9 Mio" & Chr(13) & Chr(10) & _
        "Fläche = 543.965 Km²"
  End Function

  Private Function KText() As String
    Return "Kanada" & Chr(13) & Chr(10) & _
        "Einwohnerzahl = 30,9 Mio" & Chr(13) & Chr(10) & _
        "Fläche = 9,97 Mio Km²"
  End Function

  Private Function JText() As String
    Return "Japan" & Chr(13) & Chr(10) & _
        "Einwohnerzahl = 126,5 Mio" & Chr(13) & Chr(10) & _
        "Fläche = 377.801 Km²"
  End Function
End Module
 
Code in DataGridAutoRowHeight
 
Imports System.Reflection

Namespace KdrDatagrid.DataGridAutoRowHeight
  Public Class DataGridAutoRowHeight

    Private WithEvents mDataGrid As DataGrid
    Private mArrayList As ArrayList
    Private mHeight As Integer = 20

    Public Sub New(ByVal newDatagrid As DataGrid, _
          ByVal height As Integer)
      mDataGrid = newDatagrid
      pHeight = height
    End Sub

    Private Property pHeight() As Integer
      Get
        Return mHeight
      End Get
      Set(ByVal Value As Integer)
        mHeight = Value
      End Set
    End Property

    Private Property Item(ByVal row As Integer) As Integer
      Get
        Dim propertyInfo As PropertyInfo = _
              mArrayList(row).GetType().GetProperty("Height")
        Return CInt(propertyInfo.GetValue( _
              mArrayList(row), Nothing))
      End Get
      Set(ByVal Value As Integer)
        Dim propertyInfo As PropertyInfo = mArrayList(row). _
              GetType().GetProperty("Height")
        propertyInfo.SetValue(mArrayList(row), Value, Nothing)
      End Set
    End Property

    Private Sub InitHeights(ByVal newRow As Integer)
      Dim methodInfo As MethodInfo = mDataGrid.GetType(). _
          GetMethod("get_DataGridRows", _
             BindingFlags.FlattenHierarchy Or _
             BindingFlags.IgnoreCase Or _
             BindingFlags.Instance Or _
             BindingFlags.NonPublic Or _
             BindingFlags.Public Or _
             BindingFlags.Static)

      Dim mDGrArray As Array = _
            CType(methodInfo.Invoke(mDataGrid, Nothing), Array)
      mArrayList = New ArrayList()

      Dim Obj As Object
      For Each Obj In mDGrArray
        If Obj.ToString().EndsWith( _
              "DataGridRelationshipRow") = True Then
          mArrayList.Add(Obj)
        End If
      Next

      Dim i As Integer
      For i = 0 To mArrayList.Count - 1
        Item(i) = CInt(mDataGrid.Font.Height + 6)
      Next
      Item(newRow) = mHeight
    End Sub

    Private Sub mDataGrid_MouseDown(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.MouseEventArgs) _
            Handles mDataGrid.MouseDown
      Dim HitTestI As DataGrid.HitTestInfo = _
            mDataGrid.HitTest(New Point(e.X, e.Y))
      If HitTestI.Column >= 0 AndAlso HitTestI.Row >= 0 Then
        InitHeights(HitTestI.Row)
      End If
    End Sub
  End Class
End Namespace
 
Weitere Links zum Thema
Spaltenbreiten automatisch anpassen

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (12,1 kB) Downloads bisher: [ 598 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Mittwoch, 25. Januar 2012