views:

384

answers:

4

I have a thorny little problem where I am outputting an audit trail and need to highlight the changes to each record.

I am currently using a Linq-to-Sql data source to pull the audit data back and then displaying it in a table using a repeater which works fine.

The problem is I need to change the background colour of a cell if its value has changed from the previous record and I'm struggling to find a way to do this.

An alternative I've considered is to simply generate the whole table which will work but I just get the feeling there must be an easier way to do this.

Any thoughts?

+2  A: 

You can use the ItemDataBound event to check the values of the items as they're bound. At this point you can check the value against the previous cell and if it is different add a CssClass to it that has the formatting you want.

Shawn Steward
I thought about doing that but ran into the issue of how to check the value in the corresponding cell in the previous row.Any suggestions on how to do the check?
Nathan
In the past I've been able to access the GridView object in the ItemDataBound, so all you really need is to figure out the current rowindex in there and then compare off the correpsonding row in the GridView object. Try e.rowindex
scottschulthess
A: 

Select the collection of data items and save it to a class variable with a protected get property. The collection of data items needs to be keyed to the index. Then databind the GridView to the collection of data items at runtime.

Then you should be able to access the previous item in asp.net with a tag something like

<%# this.DataItems[Container.DataItemIndex] == this.DataItems[Container.DataItemIndex] ? "same as previous item" : "different than previous item" %>
Yoenhofen
A: 

Create a field in your page class:

private int previousValue;

Then in the ItemCreated or ItemDataBound event:

int currentValue = << get current value >>;

if (previousValue != currentValue) {
  << do highlight >>;
  previousValue = currentValue;
}
Ray
A: 

After must playing about I found that it was quickest to generate the html manually.

I forgot to mention in the original question that I had to reverse the order to the records (i.e. most recent first) and therefore it became even more difficult to style the record in a repeater event e.g. add first record which has to have colour coded cells to indicate changes but at this point the next record hasn't been written so you have nothing to compare against.

The easiest way seemed to be to loop through the records in the code behind and do the comparison there property to property and then to create the html with styling based on that.

Thank you to everybody who took the time to look at the question and suggest solutions.

Nathan