Setting Back Color on a DataGrid created using DataGridTableStyle

jdallen75
jdallen75 used Ask the Experts™
on
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.
Sample-Queue.jpg
Comment
Watch Question

Do more with

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

Author

Commented:
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
        DataGridQueueStyle.GridColumnStyles.Add(GridCSNStyle)

        GridTrackStyle = New DataGridTextBoxColumn
        GridTrackStyle.MappingName = "Tracking"
        GridTrackStyle.HeaderText = "Tracking #"
        GridTrackStyle.Width = 75
        GridTrackStyle.Alignment = HorizontalAlignment.Center
        DataGridQueueStyle.GridColumnStyles.Add(GridTrackStyle)

        GridGroupIdStyle = New DataGridTextBoxColumn
        GridGroupIdStyle.MappingName = "GroupId"
        GridGroupIdStyle.HeaderText = "Rack ID"
        GridGroupIdStyle.Width = 75
        GridGroupIdStyle.Alignment = HorizontalAlignment.Center
        DataGridQueueStyle.GridColumnStyles.Add(GridGroupIdStyle)

        GridPositionStyle = New DataGridTextBoxColumn
        GridPositionStyle.MappingName = "Position"
        GridPositionStyle.HeaderText = "Pos"
        GridPositionStyle.Width = 40
        GridPositionStyle.Alignment = HorizontalAlignment.Center
        DataGridQueueStyle.GridColumnStyles.Add(GridPositionStyle)

        GridGroupColourStyle = New DataGridTextBoxColumn
        GridGroupColourStyle.MappingName = "BackColor"
        GridGroupColourStyle.HeaderText = "BackColor"
        GridGroupColourStyle.Width = 70
        GridGroupColourStyle.Alignment = HorizontalAlignment.Center
        DataGridQueueStyle.GridColumnStyles.Add(GridGroupColourStyle)
    End Sub
HainKurtSr. System Analyst

Commented:
here is the code, I guess above sample is for columns

http://www.highoncoding.com/Articles/141_Changing_GridView_Row_Color.aspx
HainKurtSr. System Analyst

Commented:
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.

Author

Commented:
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
Commented:
have a look at this samples

http://msdn.microsoft.com/en-us/library/ms996485.aspx

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

Commented:
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), _
       e.CurrentCellValue)) 
    ' 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

Author

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

Commented:
Try

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

Author

Commented:
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