Ticket Q584172
Visible to All Users

How to loop through all rows in an XtraReport

created 11 years ago

The table cells in myy report is color coded. Say, the BackColor is green if the text in the XRTableCell is "Approved". I have coded a bunch of formatting rules and successfully implemented it.
Now I am writing a test for this function. All I need is to obtain a TableCell's text and its BackColor to confirm if the formatting rule is correctly applied.

I can get the total rows in report.RowCount, and the current row by report.GetCurentRow().
Question 1.
In the debugging, I can see all the column names and their values when I expand report.GetCurrentRow(). But I could not get the value by report.GetCurentRow()[0] or report.GetCurrentRow().MyColumn.
There is a KB ar ticle: https://documentation.devexpress.com/#xtrareports/DevExpressXtraReportsUIXtraReportBase_GetCurrentColumnValuetopic
It won't work because "DataRowView" is not valid in this scope. ???
Question 2:
How can I loop through the rows?
I find no report.Rows() or report.GetAllRows() which return a collection so that I can do rows[i]. And there is no NextRow() method either.
Please help me out. Thanks!

-----------
And there is a FindControl() method. I can return the XRTableCell I want, but there is no row info and returns to me no value.

Comments (2)

    My version is v13.2.
    There is no Rows property in XRTable.

      Here in this article: https://documentation.devexpress.com/#XtraReports/DevExpressXtraReportsUIXRTable_Rowstopic
      There is Rows in XRTable. So what went wrong?

      Answers approved by DevExpress Support

      created 11 years ago (modified 11 years ago)

      Hi Blaise,
      The GetCurrentRow method returns the currently processed data entry from the underlying data source. For example, this method will return a DataRowView instance in case of DataSets/DataTables or a custom class instance if your report is bound to a collection of custom objects.
      So, to access a field or inner property, cast the GetCuurentRow result to an appropriate type.
      As for the second question, the best way to iterate through all data rows is to use data source methods. For example, you can use the 'foreach' loop to iterate through a DataTable.Rows collection or List<CustomClass>. The XtraReport class provides you with capability to get the current, next, and previous values only (GetCurrentColumnValue, GetNextColumnValue, GetPreviousColumnValue).
      I hope my explanation is clear.

        Show previous comments (2)
        DevExpress Support Team 11 years ago

          Hi Blaise,
          >>
          When I write tests, there would be no _BeforePrint() method I can use, isn't it?
          <<
          The BeforePrint event fires every time you create a report document. So, if you call the Report.CreateDocument method, this event will fire.
          >>
          How can I use GetCurrentRow on a XRReport object?
          <<
          Review the "XtraReportBase.GetCurrentRow Method" help topic.
          >>
          and table.Controls is supposed to be a collection of XRTableRow's. Isn't it?
          <<
          To get an XRTable row collection, use the XRTable.Rows property instead. Please note that the FindControl method returns an XRControl object, so it is necessary to cast it to an appropriate type. For example:

          C#
          XRTable table = report.FindControl("xrTable1", true) as XRTable; foreach (XRTableRow row in table.Rows) foreach (XRTableCell cell in row.Cells) ....

          >>
          I have to test if the actual BackColor of a TableCell is equal to the expected BackColor.
          <<
          You can use the XRTableCell.GetEffectiveBackColor method to obtain the applied background color.
          I also suggest that you consider using the PrintOnPage event. This event fires when a document has been generated and FormattingRules have been applied.

            It appears I were checking the design view!
            There is only one row in the XRTable. And the Text of XRTableCell is just the default value. Not the values we populated in the report.

            DevExpress Support Team 11 years ago

              Hi Blaise,
              As I mentioned before, the best way to iterate through all rows is to use data source methods. Would you please clarify how you want to test this function? It would be great if you could provide me with a sample project to demonstrate your approach.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.