• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1167
  • Last Modified:

DataGridView Backcolor or rowheader help

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

0
Midnight2005
Asked:
Midnight2005
1 Solution
 
mydasxCommented:
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
0
 
Midnight2005Author Commented:
Hi,
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
Thanks
0
 
Bob LearnedCommented:
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

    Me.CreateSampleData()

    Me.ConfigureGrid()

  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
      table.Rows.Add(sample)
    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)
    Else
      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

    brush.Dispose()

  End Sub
End Class

Bob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now