Hi. I am trying to shade alternate rows in a table with different colors. I got the code from the Programmers guide, but it’s throwing an exception. The table I am trying to change the colors on is ‘Peer Ownership top10’ in the attached document. Please help.
The code is below:
private void HandleMergeFieldAlternatingRows(object sender, MergeFieldEventArgs e)
{
string fieldName = e.FieldName.ToLower();
// This way we catch the beginning of a new row.
if (fieldName.Equals("top10_ticker"))
{
// Select the color depending on whether the row number is even or odd.
Color rowColor;
if (IsOdd(rowIdx))
rowColor = Color.Azure;
else
rowColor = Color.BlanchedAlmond;
// There is no way to set cell properties for the whole row at the moment,
// so we have to iterate over all cells in the row.
for (int colIdx = 0; colIdx <9; colIdx++)
{
docBuilder.MoveToCell(0, rowIdx, colIdx, 0);
docBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor;
}
rowIdx++;
}
}
///
/// Returns true if the value is odd; false if the value is even.
///
private static bool IsOdd(int value)
{
// The code is a bit complex, but otherwise automatic conversion to VB does not work.
return ((value / 2) * 2).Equals(value);
}