DataGridView Backcolor or rowheader help

Posted on 2006-05-16
Last Modified: 2008-01-09
I have an empty crosstab with only headers and I am trying to have the column header and my rows in first column to have the same background color or same appearance. Because I couldn't find a way to write to row header.

This is what I tried and didn't work for me...Any suggestions?

        Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.AntiqueWhite
        Me.DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.AntiqueWhite

Question by:Midnight2005
    LVL 5

    Expert Comment

    please advise.

    have you set the alternatingrowsdefaultcellstyle?

    if so, you can always do custom paints of rows to be what ever you want them to be by overriding like so.  In this case i am making the backgroud color pink for rows who's Cell("deleted") is true.  In your case you would not need to do any type of a if statement... just set the backcolor of the cell

    Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            Dim deleted As Boolean = CBool(Me.DataGridView1.Rows(e.RowIndex).Cells("Deleted").Value)
            If (deleted) Then
                e.CellStyle.BackColor = Color.Pink
            End If
        End Sub
    LVL 1

    Author Comment

    I don't have AlternatingRowsDefaultCellStyle in my code right now, but I was actually considering to put it.

    The code you gave would paint the cell, but again the column header color and the cell would look different. I want my column header and all the cells under first column (cells in each row) to look like heading so they look a like. Or another solution would be to write to row header because it looks like column header.

    I hate cross tab.

    I am using VS 2005
    LVL 96

    Accepted Solution

    Here is an example of custom cell painting:

    Public Class formDataGridViewPainter

      Private Sub formDataGridViewPainter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



      End Sub

      Private Sub CreateSampleData()

        Dim table As New DataTable

        For col As Integer = 0 To 2
          table.Columns.Add("Column " & col)
        Next col

        For row As Integer = 0 To 9
          Dim sample As DataRow = table.NewRow()
          For col As Integer = 0 To 2
            sample(col) = row.ToString() & ", " & col.ToString()
          Next col
        Next row

        table.DefaultView.AllowNew = False

        Me.gridExample.DataSource = table

      End Sub

      Private Sub ConfigureGrid()

        Me.gridExample.BackgroundColor = Color.AliceBlue

      End Sub

      Private Sub gridExample_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles gridExample.CellPainting

        Dim backColor As Color = Me.gridExample.DefaultCellStyle.BackColor
        Select Case e.ColumnIndex
          Case -1
            If e.RowIndex = -1 Then
              backColor = Color.Black
            End If
          Case 0
            backColor = Color.Pink

          Case 1
            backColor = Color.Yellow

          Case 2
            backColor = Color.Green

        End Select

        Dim brush As New SolidBrush(backColor)

        e.Handled = True
        Dim cell As DataGridViewCell = Me.gridExample.CurrentCell

        Dim isSelected As Boolean = cell.ColumnIndex = e.ColumnIndex AndAlso cell.RowIndex = e.RowIndex

        If isSelected Then
          e.Graphics.FillRectangle(SystemBrushes.Highlight, e.CellBounds)
          e.Graphics.FillRectangle(Brush, e.CellBounds)
        End If
        Dim sf As New StringFormat()
        sf.LineAlignment = StringAlignment.Center
        Dim rect As Rectangle = e.CellBounds
        rect.Offset(2, 0)
        e.Graphics.DrawString(e.Value, e.CellStyle.Font, Brushes.Black, rect, sf)

        If e.RowIndex = -1 Then
          e.Graphics.DrawLine(Pens.Black, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
        End If

        If e.ColumnIndex = -1 Then
          e.Graphics.DrawLine(Pens.Black, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
        End If


      End Sub
    End Class


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now