How to retrieve the values of the previous row during ItemDataBound

A

adiel

Hello, I am trying to retrieve the values of the previous row. I want
to change the color of a cell for the current row if the previous
row's cell had a different value:

Private Sub Datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
Datagrid1.ItemDataBound
Dim x As String
Dim y As String
Dim z As String

x = e.Item.Cells(0).Text
y = e.Item.Cells(1).Text
z = e.Item.Cells(2).Text
End Sub

I know how to retrieve the values of the current row as shown above.
I also know that you can temporarily store the values of the previous
row in an object such as a global string but, I would rather not do
that. How do I retrieve the values of the previous row through the
microsoft classes?

Thanks before hand,
Adiel
 
A

adiel

Hello again, I figured it out! Here is the code in case any one needs
it:

Private Sub Datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
Datagrid1.ItemDataBound

' Declare variables
Dim x As Integer
Dim y As String
Dim z As String

' Start checking every row except header (-1) and first row
(0)
If e.Item.ItemIndex > 0 Then
' Loop through every cell that we would like to check for
changes
For x = 2 To 5
' Get current row cell
y = e.Item.Cells(x).Text
' Get previous row cell
z = Datagrid1.Items.Item(e.Item.ItemIndex -
1).Cells(x).Text
' If the value of the current cell has changed from
the value of
' the previous cell, change the color of the current
cell
If y <> z Then
e.Item.Cells(x).BackColor = Color.Red
End If
Next
End If
End Sub

Thanks,
Adiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top