Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to program the color of a datagrid cell

Status
Not open for further replies.

jasperjasper

Programmer
Mar 23, 2004
117
US
I am building an app that utilizes the datagrid control.
I am also trying to learn how to program it. I am having some trouble getting a cell to color depending on the amount. If the balance is > than 0 I want it red else green.

I am not sure of the exact syntax. I have control of the cell amount when i load it...just cant get the color.
 

U can override Paint event of DataGridTextBoxColumn. The following sample requires you to bind your DataGrid to DataView

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
'Instantiate a DataRowView so we can examine a row
'of the DataView associated with the DataGrid
Dim drv As DataRowView
'Get a handle on the DataView being used by the CurrencyManager
Dim dv As DataView = CType(source.List, DataView)
'Get the DataRowView of the row being painted.
drv = CType(dv.Item(rowNum), DataRowView)
'Use column of the DataRowView to determine the formatting of the
'column this object is painting.
If drv("ColumnName") > 0 Then
' Here is where we are going to do the actual painting.
backBrush = Brushes.Red
foreBrush = Brushes.Yellow
Else
backBrush = Brushes.White
foreBrush = Brushes.Black
End If

MyBase.Paint(g, bounds, source, rowNum, _
backBrush, foreBrush, alignToRight)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top