Tipp 0095 DataGrid-Spalten farbig unterlegen
Autor/Einsender:
Datum:
  Klaus Raudszus
11.12.2005
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Dieser Tipp zeigt, wie man über eine Komponentenklasse das Datagrid mit Hilfe von Vererbung des Basis-Controls und durch Überschreibung des Paint-Events um die Möglichkeit der Spaltencolorierung erweitern kann.
Auf diese Weise lassen sich einzelne Spalten im DataGrid farblich voneinander absetzen.
Code in comDGColoredColumn
 
Public Class comDGColoredColumn
  Inherits DataGridTextBoxColumn

  Private m_Color As Color = Color.Red

  Public Sub New()
  End Sub

  Public Property NewColor() As Color
    Get
      Return m_Color
    End Get
    Set(ByVal Value As Color)
      m_Color = Value
    End Set
  End Property

  Protected Overloads Overrides Sub Paint( _
          ByVal g As Graphics, _
          ByVal bounds As Rectangle, _
          ByVal source As CurrencyManager, _
          ByVal rowNum As Integer, _
          ByVal backBrush As Brush, _
          ByVal foreBrush As Brush, _
          ByVal alignToRight As Boolean)

    backBrush = New SolidBrush(m_Color)
    MyBase.Paint(g, bounds, source, rowNum, backBrush, _
          foreBrush, alignToRight)
  End Sub
End Class
 
Code in frmMain
 
Public Class frmMain
  Inherits System.Windows.Forms.Form

  Friend WithEvents mDataSet As DataSet

  Public Shared Sub Main()
    Application.Run(New frmMain)
  End Sub

  Public Sub StartUp()
    Call CreateTableStyle()
    Call CreateDataSet()
    dgDataGrid.ReadOnly = True
    dgDataGrid.Select()
  End Sub

  Private Sub CreateDataSet()
    Dim i As Integer
    mDataSet = New DataSet("newDataSet")

    Dim mDTable As DataTable = New DataTable("Kunden")
    Dim dcID As DataColumn = _
           New DataColumn("ID", GetType(System.Int32))
    Dim dcName As DataColumn = New DataColumn("Name")
    mDTable.Columns.AddRange(New DataColumn() {dcID, dcName})
    mDataSet.Tables.Add(mDTable)

    Dim newRow As DataRow
    For i = 0 To 6
      newRow = mDTable.NewRow
      newRow("ID") = i + 1
      mDTable.Rows.Add(newRow)
    Next

    mDTable.Rows(0)("Name") = "Müller"
    mDTable.Rows(1)("Name") = "Lehmann"
    mDTable.Rows(2)("Name") = "Huber"
    mDTable.Rows(3)("Name") = "Schulze"
    mDTable.Rows(4)("Name") = "Meier"
    mDTable.Rows(5)("Name") = "Mustermann"
    mDTable.Rows(6)("Name") = "Schumann"
    dgDataGrid.DataSource = mDTable
  End Sub

  Private Sub CreateTableStyle()
    Dim dgTS As DataGridTableStyle = New DataGridTableStyle
    With dgTS
      .MappingName = "Kunden"
    End With

    Dim IdCol As comDGColoredColumn = New comDGColoredColumn
    With IdCol
      .MappingName = "Id"
      .HeaderText = "Kd-Nr."
      .Width = 50
      .Alignment = HorizontalAlignment.Center
      .NewColor = Color.LightGreen
    End With
    dgTS.GridColumnStyles.Add(IdCol)

    Dim NameCol As comDGColoredColumn = New comDGColoredColumn
    With NameCol
      .MappingName = "Name"
      .HeaderText = "Name"
      .NewColor = Color.LightBlue
    End With
    dgTS.GridColumnStyles.Add(NameCol)
    dgDataGrid.TableStyles.Add(dgTS)
  End Sub
End Class
 
Weitere Links zum Thema
DataGrid-Zellen farbig unterlegen

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


Download  (8,1 kB) Downloads bisher: [ 655 ]

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: Sonntag, 8. Januar 2012