Tuesday, June 1, 2010

How to iterate on Tablix rows?

I have implemented a custom renderer for sql server 2005 in order to send direct information from a report to Microsoft Dynamics CRM. When they announced me that I have to port the solution to sql server 2008 I encountered the following problems:

1. The namespace have been changed from

Microsoft.ReportingServices.ReportRendering;

to

Microsoft.ReportingServices.OnDemandReportRendering;

2. Control Table did not exist and had been replaced with Tablix. The iteration of table rows was easy, and I have implement it like this

Table table = reportItem as Table;

for (int j = 0; j < table.DetailRows.Count; j++)
{
    TableDetailRowCollection tableDetailRow = table.DetailRows[j];
    for (int k = 0; k < tableDetailRow.Count; k++)
    {
       
    }
}

But the Tablix control does not work like this. In order to iterate through the tablix rows I implemented the following code

Tablix table = reportItem as Tablix;                                               

int pos = -1;
foreach (TablixMember memberDef in table.RowHierarchy.MemberCollection)
{
    pos++;
    if (memberDef.IsStatic)
        continue;
    TablixDynamicMemberInstance instance = (TablixDynamicMemberInstance)memberDef.Instance;
    instance.ResetContext();
    while (instance.MoveNext())
    {
        foreach (TablixCell tableCell in table.Body.RowCollection[pos])
        {
            if (tableCell.CellContents.ReportItem is TextBox)
            {
                ...
            }
        }                               
    }
}