jdallen75
asked on
Setting Back Color on a DataGrid created using DataGridTableStyle
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
Sample-Queue.jpg
here is the code, I guess above sample is for columns
http://www.highoncoding.com/Articles/141_Changing_GridView_Row_Color.aspx
http://www.highoncoding.com/Articles/141_Changing_GridView_Row_Color.aspx
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...
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...
ASKER
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???
I see where you are getting at.. but just not finding the right call to get there.... DataGrid.DataBindings or whatever???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
Ughh... I'm following along in that article, and a little confused.... where does the code snippet below "Color Formatting" go?
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
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
ASKER
Thanks for the input, ashok111.... I realized that once I downloaded the sample project.
ASKER
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.Mapping
DataGridQueueStyle.HeaderF
("Arial", 9.25F, System.Drawing.FontStyle.B
System.Drawing.GraphicsUni
DataGridQueueStyle.RowHead
DataGridQueueStyle.ColumnH
DataGridQueueStyle.Preferr
DataGridQueueStyle.GridLin
DataGridQueueStyle.HeaderB
GridCSNStyle = New DataGridTextBoxColumn
GridCSNStyle.MappingName = "CSN"
GridCSNStyle.HeaderText = "CSN"
GridCSNStyle.Width = 75
GridCSNStyle.Alignment = HorizontalAlignment.Center
DataGridQueueStyle.GridCol
GridTrackStyle = New DataGridTextBoxColumn
GridTrackStyle.MappingName
GridTrackStyle.HeaderText = "Tracking #"
GridTrackStyle.Width = 75
GridTrackStyle.Alignment = HorizontalAlignment.Center
DataGridQueueStyle.GridCol
GridGroupIdStyle = New DataGridTextBoxColumn
GridGroupIdStyle.MappingNa
GridGroupIdStyle.HeaderTex
GridGroupIdStyle.Width = 75
GridGroupIdStyle.Alignment
DataGridQueueStyle.GridCol
GridPositionStyle = New DataGridTextBoxColumn
GridPositionStyle.MappingN
GridPositionStyle.HeaderTe
GridPositionStyle.Width = 40
GridPositionStyle.Alignmen
DataGridQueueStyle.GridCol
GridGroupColourStyle = New DataGridTextBoxColumn
GridGroupColourStyle.Mappi
GridGroupColourStyle.Heade
GridGroupColourStyle.Width
GridGroupColourStyle.Align
DataGridQueueStyle.GridCol
End Sub