Locations of visitors to this page
Compact Framework DataGrid Alternating Color

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Compact Framework DataGrid Alternating Color

One of the things I miss from the Windows Forms DataGrid is the ability to have the rows alternating in color.  For this example I created a custom DataGridTextBox column which will display each other row in an different color. Basically I override the paint method to call the existing paint method with a different back color brush for every other column.

 

Public Class AltColorColumn
    Inherits DataGridTextBoxColumn

    Private m_AltColor As Color
    Public Property AltColor() As Color
        Get
            Return m_AltColor
        End Get
        Set(ByVal value As Color)
            m_AltColor = value
        End Set
    End Property

    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
        If rowNum Mod 2 = 0 Then
            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
        Else
            Dim bb As New SolidBrush(m_AltColor)
            MyBase.Paint(g, bounds, source, rowNum, bb, foreBrush, alignToRight)
            bb.Dispose()
        End If
    End Sub

    Public Sub New()
        m_AltColor = Color.LightSkyBlue
    End Sub
End Class

 

To test the column style I added the Northwind Sql Compact edition database and let it create a typed dataset for the products table for me.  While compiling the project I got a few errors in the designer generated code for the typed dataset. The compact framework does not support the dispose method for the memory stream.  You can comment out the few lines of code safely

 

Finally
    If (Not (s1) Is Nothing) Then
        's1.Dispose
    End If
    If (Not (s2) Is Nothing) Then
        's2.Dispose
    End If
End Try

 

Here is the code I used to generate a datagrid with alternating color columns.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bs As New BindingSource
        Dim dt As New NorthwindDataSet.ProductsDataTable
        Dim ta As New NorthwindDataSetTableAdapters.ProductsTableAdapter

        ta.Fill(dt)

        bs.DataSource = dt

        Dim ts As New DataGridTableStyle
        ts.MappingName = dt.TableName

        Dim colName As New AltColorColumn
        With colName
            .Width = 150
            .HeaderText = "Product Name"
            .MappingName = "Product Name"
        End With

        Dim colPrice As New AltColorColumn

        With colPrice
            .Width = 75
            .HeaderText = "Price"
            .MappingName = "Unit Price"
            .Format = "c"
        End With

        ts.GridColumnStyles.Add(colName)
        ts.GridColumnStyles.Add(colPrice)
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = bs
    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Vista | Compact Framework
Posted by Ken Tucker on Friday, October 19, 2007 11:01 AM
Permalink | Comments (1) | Post RSSRSS comment feed

Related posts

Comments

Paul

Saturday, January 05, 2008 11:20 AM

Paul

great article, works like a charm!
well done, and thanks for this.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Friday, July 04, 2008 5:14 AM