Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

Runtime table creation best practices (iterative approach)

$
0
0

This example demonstrates the differences in runtime table creation between the newest XtraReports version and its prior versions.

Please note that it is always required to call the XRTable.BeginInit and XRTable.EndInit methods if you modify XRTable.Rows and XRTableRow.Cells collections at runtime.

As for the height of a table, explicitly specify it only if cells' content is not expected to stretch the cells (e.g. this may happen when their CanGrow property is enabled).

See also:
How to convert an XtraGrid to an XtraReport at runtime

Question Comments

Added By: DXScorpion at: 10/14/2013 3:12:53 AM    

Your sample is too simple.
In case datasource has many columns and total width of columns greater than (Report.PageWidth - Report.Margins.Left - Report.Margins.Right) How can we show all of it on report?

Added By: Leon Zeng at: 9/19/2014 5:55:49 AM    

The example works fine.  However,
(1) It's better to use gridColumn's width, caption and fieldName
(2) When dataset has more than 1 table, the cells show empty. Below line fixes it:
               cell2.DataBindings.Add("Text", null, tableName + "." + columns[i].FieldName);  
(3) Could you please show example of sumary row/column as well?
E.g., lastcol = col5 + col6 + col7,
lastRow(col5) = sum(col5), etc.

Added By: Leon Zeng at: 9/19/2014 6:36:03 AM    

code snipet of my test below.  

       private void InitTables(List<GridColumn> columns)
       {
           int colCount = columns.Count;
           int bodyWidth = (PageWidth - (Margins.Left + Margins.Right));
    
           //header
           XRTable table = new XRTable();
           XRTableRow row = new XRTableRow();
           //cells
           XRTable table2 = new XRTable();
           XRTableRow row2 = new XRTableRow();
           for (int i = 0; i < colCount; i++)
           {
               XRTableCell cell = new XRTableCell();
               cell.Width = (int)columns[i].Width;
               cell.Text = columns[i].Caption;
               cell.Font = new Font(cell.Font, FontStyle.Bold);
               if (i > 0)
                   cell.Borders = BorderSide.Top | BorderSide.Right | BorderSide.Bottom;
               else
                   cell.Borders = BorderSide.All;
               cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
               row.Cells.Add(cell);

               XRTableCell cell2 = new XRTableCell();
               if (i > 0)
                   cell2.Borders = BorderSide.Right | BorderSide.Bottom;
               else
                   cell2.Borders = BorderSide.Right | BorderSide.Bottom | BorderSide.Left;
               cell2.Padding = new PaddingInfo(5, 5, 5, 5);
               cell2.Width = columns[i].Width;
               cell2.DataBindings.Add("Text", null, tableName + "." + columns[i].FieldName);
               if(columns[i].OptionsColumn.AllowMerge == DevExpress.Utils.DefaultBoolean.True)
                   cell2.ProcessDuplicates = DevExpress.XtraReports.UI.ValueSuppressType.MergeByValue;
               row2.Cells.Add(cell2);

           }

           table.Rows.Add(row);
           table.Width = bodyWidth;

           table2.Rows.Add(row2);
           table2.Width = bodyWidth;

           Bands[BandKind.PageHeader].Controls.Add(table);
           Bands[BandKind.Detail].Controls.Add(table2);
           for (int i = 0; i < colCount; i++)
           {
               if (columns[i].SummaryItem.SummaryType != null)
               {
                   showSummaryRow = true;
                   break;
               }
           }

           if (showSummaryRow)
           {
               XRTable table3 = new XRTable();
               XRTableRow row3 = new XRTableRow();
               table3.Rows.Add(row3);
               table3.Width = bodyWidth;

               for (int i = 0; i < colCount; i++)
               {
                   //summary

                   XRTableCell cell3 = new XRTableCell();
                   cell3.Padding = new PaddingInfo(5, 5, 5, 5);
                   cell3.Width = columns[i].Width;
                   cell3.BackColor = System.Drawing.Color.AliceBlue;

                   if (i > 0)
                   {
                       cell3.Borders = BorderSide.Right | BorderSide.Bottom;
                       cell3.DataBindings.Add("Text", null, tableName + "." + columns[i].FieldName);
                       DevExpress.XtraReports.UI.XRSummary xrSummary = new DevExpress.XtraReports.UI.XRSummary();
                       xrSummary.FormatString = "{0:#}";
                       xrSummary.Running = DevExpress.XtraReports.UI.SummaryRunning.Report;
                       xrSummary.Func = getFunc(columns[i].SummaryItem.SummaryType);
                       cell3.Summary = xrSummary;
                   }
                   else
                   {
                       cell3.Borders = BorderSide.Right | BorderSide.Bottom | BorderSide.Left;
                       cell3.Text = "合计";
                   }
                   row3.Cells.Add(cell3);
               }
               Bands[BandKind.ReportFooter].Controls.Add(table3);
           }
       }

       private DevExpress.XtraReports.UI.SummaryFunc getFunc(SummaryItemType type){
           switch (type)
           {
               case SummaryItemType.Sum: return SummaryFunc.Sum;
               case SummaryItemType.Count: return SummaryFunc.Count;
               case SummaryItemType.Average: return SummaryFunc.Avg;
               case SummaryItemType.Max: return SummaryFunc.Max;
               case SummaryItemType.Min: return SummaryFunc.Min;
               default: return SummaryFunc.Count;
           }
       }


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>