Megalith
Well-known member
- Joined
- Aug 21, 2006
- Messages
- 66
- Programming Experience
- 10+
I'm working on a database program that needs to conditionally change backcolor of a cell based on the value of a cell in the same row, i spent hours searching online for a way to do this and could only find things relating to the DataGrid so i looked on MSDN and found a routine that could change background colours i modified it slightly for my data set and created a simple form with a DataGridView object on it which was bound at design time to my database. it seemed to work (after replacing column names with the column number) but i noticed a few things wrong the backcolor of the headers was still white and the backcolor of the column i changed remained unaltered. Heres the code in my form 1, can anybody see what i have done wrong and does anyone know how to conditionally change an individual cells backcolor property?
I read in my research that this could only be done thru a paint method but the source of my info was pre .NET 2, MSDN appears to indicate it is possible to conditionally change cells.
VB.NET:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'HandHistoryDataSet.HoldEm' table. You can move, or remove it, as needed.
InitializeDataGridView()
Me.HistoryTableAdapter.Fill(Me.HistoryDataSet.HoldEm)
End Sub
' Configures the appearance and behavior of a DataGridView control.
Private Sub InitializeDataGridView()
' Initialize basic DataGridView properties.
dataGridView1.Dock = DockStyle.Fill
dataGridView1.BackgroundColor = Color.LightGray
DataGridView1.BorderStyle = BorderStyle.Fixed3D
' Set property values appropriate for read-only display and
' limited interactivity.
dataGridView1.AllowUserToAddRows = False
dataGridView1.AllowUserToDeleteRows = False
dataGridView1.AllowUserToOrderColumns = True
dataGridView1.ReadOnly = True
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dataGridView1.MultiSelect = False
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
dataGridView1.AllowUserToResizeColumns = False
dataGridView1.ColumnHeadersHeightSizeMode = _
DataGridViewColumnHeadersHeightSizeMode.DisableResizing
dataGridView1.AllowUserToResizeRows = False
dataGridView1.RowHeadersWidthSizeMode = _
DataGridViewRowHeadersWidthSizeMode.DisableResizing
' Set the selection background color for all the cells.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
DataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
' Set the background color for all rows and for alternating rows.
' The value for alternating rows overrides the value for all rows.
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray
' Set the row and column header styles.
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black
' change the foreground and background color of this column
DataGridView1.Columns(1).DefaultCellStyle.ForeColor = Color.Red
DataGridView1.Columns(1).DefaultCellStyle.BackColor = Color.AliceBlue
' Specify a larger font for the "Ratings" column.
Dim font As New Font( _
DataGridView1.DefaultCellStyle.Font.FontFamily, 14, FontStyle.Bold)
Try
DataGridView1.Columns(14).DefaultCellStyle.Font = font
Finally
font.Dispose()
End Try
End Sub
End Class
Last edited: