# .NET and More > WPF, WCF, WF >  WPF DataGrid Help

## Brian Henry

Hi All

I want to loop through a datagrid and change the row color if a cell has a value is greater then my alarm value

Sum thing like this 


```

Dim Alarm1 as string = "245.00"

For Each band As Forms.DataGridViewBand In DG1.Items
                If DG1.Items(band.Index).Cells("Down Stream").Value > Alarm1 Then
                    band.DefaultCellStyle.BackColor = System.Drawing.Color.Red
                End If
            Next
```

----------


## Flashbond

You should loop through Rows, not Bands:



```
Dim Alarm1 as Double = 245.00
        For Each row As DataGridViewRow In DG1.Rows
            If row.Cells("Down Stream").Value > Alarm1 Then
                row.DefaultCellStyle.BackColor = Color.Red
            End If
        Next
```

EDIT: Aso String is not the appropriate datatype to compare values. It is not a value. You know, string is just a string. It's not really a "number". If you have to use it as a string then you have to cast it to a Double in order to make it comparable with other values:


```
Dim Alarm1 as String = "245.00"
        For Each row As DataGridViewRow In DG1.Rows
            If row.Cells("Down Stream").Value > CDbl(Alarm1) Then
                row.DefaultCellStyle.BackColor = Color.Red
            End If
        Next
```

----------


## dday9

_Moderator Action: Moved thread to WPF forums_

----------


## Brian Henry

I'm getting an error on DG1.Rows ('Rows' is not a member of datagrid)

----------


## Flashbond

Aahh, becausw my code is for DataGridView. I think you should use bands then. I don't remember exactly the thing for DataGrid.

----------


## KGComputers

Using the extension method GetRow() here:
How-to-set-DataGrid-Cell-Color.

Here's how you implement it.

vb.net Code:
'paint entire row code
For index = 0 To grid1.Items.Count - 1 Step 1
       Dim firstRow As DataGridRow = grid1.GetRow(index)
       ' Cast to DataRowView, since my datagrid's itemsource property is of type DataTable.
       ' Replace DirectCast if your ItemSource is a class.   
       Dim item As DataRowView = DirectCast(firstRow.Item, DataRowView)
       If item(0).ToString().Equals("245.00") Then
              'set background
              firstRow.Background = Brushes.Red
       End If
Next

----------

