This is a migrated thread and some comments may be shown as answers.

Iterate through each row and column of the details section in a table

2 Answers 602 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Epep
Top achievements
Rank 1
Epep asked on 17 Jul 2018, 06:23 AM

Hi,

I have a report which has a large table in it with more than 40 columns. This table is bound to an ObjectDataSource element. Is it possible for me to iterate through each row and column after the elements are bound to the database (I guess I need to use the ItemDataBound event handler)? I have to make changes to the format and style of each cell of the table, which actually are textboxes.

I think I am able to do it also from design, using the conditional formatting and expressions to set the values of the textboxes in details section, but since I am migrating existing reports I don't want to do the whole work from scratch.

Thanks

2 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 19 Jul 2018, 03:28 PM
Hi Epep,

The approach we recommend to modify styling and appearance of report items is indeed with ConditionalFormatting or Bindings. Complex expressions can be used to achieve the required output based on the provided data.

Depending on the exact requirement, it may be possible to use also report events. Take into account that we do not guarantee that modifications made in report events will work with future versions of Telerik Reporting, as our API may change. Check also the Changes on items in report events are not applied KB article.

Here is a sample code in the ItemDataBound event handler of a Table that demonstrates how to iterate through table cells and change their background color :
private void table1_ItemDataBound(object sender, EventArgs e)
{
    Telerik.Reporting.Processing.Table table = (Telerik.Reporting.Processing.Table) sender;
    int rowsCount = table.Rows.Count;
    int columnsCount = table.Columns.Count;
 
    for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
    {
        for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)
        {
            int tableIndex = rowIndex + columnIndex;
            var cell = table.GetCell(rowIndex, columnIndex);
            Color color;
 
            switch (tableIndex)
            {
                case 0:
                    color = Color.Aqua;
                    break;
                case 1:
                    color = Color.DarkRed;
                    break;
                case 2:
                    color = Color.RosyBrown;
                    break;
                case 3:
                    color = Color.Goldenrod;
                    break;
                case 4:
                    color = Color.Honeydew;
                    break;
                default:
                    color = Color.GreenYellow;
                    break;
            }
 
            cell.Item.Style.BackgroundColor = color;
        }
    }
}

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Epep
Top achievements
Rank 1
answered on 19 Jul 2018, 07:44 PM

Hi Todor,

 

Thanks for your reply. Yes, I saw that the best option was to for the Conditional Formatting and add there all necessary conditions. But the problem I faced was in regards to the merging of the columns if a certain condition was met. But in this case, the Group Explorer came to help. So, I created in design more than one rows and then I show/hide one row depending on the conditions. Now the report works very well.

But also the code that you provided, is very helpful to understand how to iterate through rows.

 

Regards

Tags
General Discussions
Asked by
Epep
Top achievements
Rank 1
Answers by
Todor
Telerik team
Epep
Top achievements
Rank 1
Share this question
or