Tipp 0067 DataGrid-Spaltenbreiten 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 Spalten in einem DataGrid automatisch auf die größte Schriftweite darin erweitert oder verkleinert.
Code in modInitDataGrid
 
Module modInitDataGrid
  Public Sub InitDataGrid(ByVal m_DataGrid As DataGrid)
    With m_DataGrid
      .ReadOnly = True
      .Size = New Size(350, 200)
      .BackgroundColor = Color.LightSalmon
      .CaptionText = "DataGrid"
      .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.Regular, _
            GraphicsUnit.Point, CType(0, Byte))
      .BackColor = Color.LightSalmon
      .AlternatingBackColor = Color.Salmon
    End With

    Dim newTextColumnId As DataGridTextBoxColumn = _
          New DataGridTextBoxColumn()
    With newTextColumnId
      .MappingName = "Id"
      .HeaderText = "Id"
      .Alignment = HorizontalAlignment.Center
    End With
    newDataGridTableStyle.GridColumnStyles.Add(newTextColumnId)

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

    Dim newTextColumnKont As DataGridTextBoxColumn = _
          New DataGridTextBoxColumn()
    With newTextColumnKont
      .MappingName = "Kont"
      .HeaderText = "Kont"
    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("Deutschland", "Europa", "D")
      .Addelement1Row("Frankreich", "Europa", "F")
      .Addelement1Row("Kanada", "Amerika", "Can")
      .Addelement1Row("Japan", "Asien", "Jap")
      .Addelement1Row("Spanien", "Europa", "Es")
      .Addelement1Row("United Kingdom", "Europa", "Uk")
      .Addelement1Row("Vereinigte Staaten", "Amerika", "Us")
    End With
    mDataGrid.Select()
    Dataset.Dispose()
  End Sub
End Module
 
Code in DataGridAutoColumnsSize
 
Namespace KdrDataGrid.DataGridAutoColumnsSize
  Public Class DataGridAutoColumnsSize

    Private mDatagrid As DataGrid
    Private mMappingName As String

    Public Sub New(ByVal newDataGrid As DataGrid, _
          ByVal strMappingName As String)
      mDatagrid = newDataGrid
      mMappingName = strMappingName
      Call AutoTableSize()
    End Sub

    Private Sub AutoTableSize()
      Dim i As Integer = 0
      Dim intColumns As Integer = _
            CType(mDatagrid.DataSource, DataTable). _
            Columns.Count - (1)
      For i = 0 To intColumns
        AutoColumnsSize(i)
      Next
    End Sub

    Private Sub AutoColumnsSize(ByVal col As Integer)
      Dim sglWidth As Single
      Dim intRows As Integer = _
            CType(mDatagrid.DataSource, DataTable). _
            Rows.Count - (1)
      Dim g As Graphics = Graphics.FromHwnd(mDatagrid.Handle)
      Dim strFormat As StringFormat = _
            New StringFormat(StringFormat.GenericDefault)
      Dim newSizeF As SizeF
      Dim i As Integer
      For i = 0 To intRows
        newSizeF = g.MeasureString(mDatagrid(i, col).ToString, _
              mDatagrid.Font, 1000, strFormat)
        If newSizeF.Width > sglWidth Then sglWidth = _
              newSizeF.Width + 5
      Next
      mDatagrid.TableStyles(mMappingName). _
            GridColumnStyles(col).Width = CInt(sglWidth)
      g.Dispose()
    End Sub

    Protected Overrides Sub Finalize()
      MyBase.Finalize()
      mDatagrid.Dispose()
    End Sub
  End Class
End Namespace
 
Weitere Links zum Thema
Zeilenhöhen automatisch anpassen

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


Download  (12 kB) Downloads bisher: [ 746 ]

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