DataGrid - formatting a cell based on criteria

toon10

Member
Joined
Jul 12, 2004
Messages
5
Programming Experience
10+
Hi



I’ve done some searching on DataGrids but I’m having trouble finding exactly what I need. Basically, I want to test each cell in a specific column for matching criteria and if matches, then change the colour of that cell.



I have found out how to test all cells but I can’t pin down a specific column. For example, I want to test my fourth column along for any cell values > 20. If they are > 20 then I want the cell to appear red. What I have with the following code is any cell in any column changing to red if it is >20. I now have the user id column going red after the first 20 users because that cell matches the criteria!


Any help would be gratefully received!

Thanks

Andrew


Here’s my code:



I’ve created a separate class…



Option Strict Off

Option Explicit On



Imports Microsoft.VisualBasic

Imports System

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms



Public Class DataGridColoredTextBoxColumn

Inherits DataGridTextBoxColumn

Public Sub New()

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)

Try

Dim o As Object

o = Me.GetColumnValueAtRow(source, rowNum)

If (Not (o) Is Nothing) Then

Dim c As Integer

c = CType(o, Integer)



If (c > 20) Then 'if ANY cell > 20 (**HERE’S WHERE I WANT SPECIFIC COLUMN!**)

backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200,

200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)

foreBrush = New SolidBrush(Color.White)

End If

End If



Catch ex As Exception

Finally

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

End Try

End Sub

End Class



The following is a routine, which populates and formats my datagrid called DGAll…



Private Sub DisplayDataGridALL()

Try

Dim DataAdapterALL As SqlDataAdapter

DataAdapterALL = New SqlDataAdapter(SelectStringALL, ConnectString)

DataAdapterALL.TableMappings.Add("Table", "Results") 'map table to results.

MyDataSet = New DataSet

DataAdapterALL.Fill(MyDataSet) 'fill the dataset.



DGAll.SetDataBinding(MyDataSet, "Results") 'bind the datagrid control to the table.



Dim tableStyle As DataGridTableStyle

tableStyle = New DataGridTableStyle

tableStyle.MappingName = "results"



Dim numCols As Integer

numCols = MyDataSet.Tables("results").Columns.Count

Dim aColumnTextColumn As DataGridColoredTextBoxColumn

Dim i As Integer

i = 0



Do While (i < numCols)

aColumnTextColumn = New DataGridColoredTextBoxColumn

aColumnTextColumn.HeaderText = MyDataSet.Tables("results").Columns(i).ColumnName

aColumnTextColumn.MappingName = MyDataSet.Tables("results").Columns(i).ColumnName

tableStyle.GridColumnStyles.Add(aColumnTextColumn)

i = (i + 1)

Loop



'datagrid display

DGAll.TableStyles.Clear()

DGAll.TableStyles.Add(tableStyle)

DGAll.DataSource = MyDataSet.Tables("results")

DGAll.CaptionText = ("All Data Results (non updateable). " & MyDataSet.Tables(0).Rows.Count & " Records.")

AutoSizeTable() 'auto size routine for the grid columns



Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical, "DGAll Error")

Return

End Try

End Sub

 
Back
Top