Alternate Rows Shading problem

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);
}

Hi

Thanks for your inquiry. I modified your code. Please try using the following code:

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(mRowIdx))
            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.
        // Get Row where mergefield is located.
        mBuilder.MoveToField(e.Field, true);
        Row currentRow = mBuilder.CurrentParagraph.GetAncestor(NodeType.Row) as Row;
        if (currentRow != null)
        {
            foreach(Cell cell in currentRow)
            cell.CellFormat.Shading.BackgroundPatternColor = rowColor;
        }
        mRowIdx++;
    }
}

I highlighted my changes.
Hope this helps.
Best regards,

That worked great. Thanks for the quick response.