Setting Back Color on a DataGrid created using DataGridTableStyle

jdallen75 used Ask the Experts™
My application has a read-only DataGrid that is defined using a DataGridTableStyle, which is defined using GridColumnStyles. I have the bound data set returning a field called BackColor, and all I simply want to do is update the back color of each ROW accordingly. I can't seem to find how to do this. Screenshot is attached.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


In case it helps, here is the sub where the DataGridTableStyle is created:

Private Sub CreateQueueStyle()
        Dim GridCSNStyle As DataGridTextBoxColumn
        Dim GridTrackStyle As DataGridTextBoxColumn
        Dim GridGroupIdStyle As DataGridTextBoxColumn
        Dim GridPositionStyle As DataGridTextBoxColumn
        Dim GridGroupColourStyle As DataGridTextBoxColumn

        DataGridQueueStyle = New DataGridTableStyle
        DataGridQueueStyle.MappingName = "ARMQueue"
        DataGridQueueStyle.HeaderFont = New System.Drawing.Font _
                    ("Arial", 9.25F, System.Drawing.FontStyle.Bold, _
                     System.Drawing.GraphicsUnit.Point, CType(0, System.Byte))
        DataGridQueueStyle.RowHeadersVisible = False
        DataGridQueueStyle.ColumnHeadersVisible = True
        DataGridQueueStyle.PreferredRowHeight = 24
        DataGridQueueStyle.GridLineColor = Color.DarkGray
        DataGridQueueStyle.HeaderBackColor = System.Drawing.Color.LightGoldenrodYellow

        GridCSNStyle = New DataGridTextBoxColumn
        GridCSNStyle.MappingName = "CSN"
        GridCSNStyle.HeaderText = "CSN"
        GridCSNStyle.Width = 75
        GridCSNStyle.Alignment = HorizontalAlignment.Center

        GridTrackStyle = New DataGridTextBoxColumn
        GridTrackStyle.MappingName = "Tracking"
        GridTrackStyle.HeaderText = "Tracking #"
        GridTrackStyle.Width = 75
        GridTrackStyle.Alignment = HorizontalAlignment.Center

        GridGroupIdStyle = New DataGridTextBoxColumn
        GridGroupIdStyle.MappingName = "GroupId"
        GridGroupIdStyle.HeaderText = "Rack ID"
        GridGroupIdStyle.Width = 75
        GridGroupIdStyle.Alignment = HorizontalAlignment.Center

        GridPositionStyle = New DataGridTextBoxColumn
        GridPositionStyle.MappingName = "Position"
        GridPositionStyle.HeaderText = "Pos"
        GridPositionStyle.Width = 40
        GridPositionStyle.Alignment = HorizontalAlignment.Center

        GridGroupColourStyle = New DataGridTextBoxColumn
        GridGroupColourStyle.MappingName = "BackColor"
        GridGroupColourStyle.HeaderText = "BackColor"
        GridGroupColourStyle.Width = 70
        GridGroupColourStyle.Alignment = HorizontalAlignment.Center
    End Sub
HainKurtSr. System Analyst

here is the code, I guess above sample is for columns
HainKurtSr. System Analyst

idea is use RowDataBound, which is called for every row,

if row is a datarow (not header or footer etc.) check the row, index, etc... based on these set

e.Row.BackColor = System.Drawing.Color.Green;

for example...
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.


Recall this is a DataGrid in VB.NET, not a GridView in ASP.NET.

I see where you are getting at.. but just not finding the right call to get there.... DataGrid.DataBindings or whatever???
Sr. System Analyst
have a look at this samples

there are lots of samples to play with row, col & cell colors... depending on cell's values...
HainKurtSr. System Analyst

here is the sample from above page... depending on the cell values, row color & style is updated:
Private Sub FormatGridRow(ByVal sender As Object, _
        ByVal e As DataGridFormatCellEventArgs)
    Dim discontinuedColumn As Integer = 0
    ' Conditionally set properties in e depending upon e.Row and e.Col.
    Dim discontinued As Boolean = CBool( _
       IIf(e.Column <> discontinuedColumn, _
       Me.DataGrid1(e.Row, discontinuedColumn), _
    ' Check if discontinued?
    If e.Column > discontinuedColumn AndAlso _
CBool(Me.DataGrid1(e.Row, discontinuedColumn)) Then 
         e.BackBrush = Me.disabledBackBrush
         e.ForeBrush = Me.disabledTextBrush
    ' current row?
    ElseIf e.Column > discontinuedColumn AndAlso _
           e.Row = Me.DataGrid1.CurrentRowIndex Then 
         e.BackBrush = Me.currentRowBackBrush
         e.TextFont = Me.currentRowFont
    End If
End Sub

Open in new window


Ughh... I'm following along in that article, and a little confused.... where does the code snippet below "Color Formatting" go?
AshokSr. Software Engineer


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)

'The code from Raising the Event & Color Formatting should go here

End Sub


Thanks for the input, ashok111.... I realized that once I downloaded the sample project.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial