Search code examples
c#methodsstimulsoft

Customiz (or Create) a table(StiTable) at runtime in C# (.NET4)


I'm working on stimulsoft 2017.2.2 and C# (.NET4). I have designed a report with footer and header , It is A4 and there is a table in it , based on stimulsofts method(DataGrid) to create a table at runtime I managed to change it to a report which accepts DataTable and everything works just fine on an empty project (.mrt file). There is a table so i want to customize it there , Call it and customize it at runtime.

The problem is when I want to add this to my own file (.mrt) the table is empty , it only has 1 column and the rows are fine , the whole table is empty but the design is visible. So I will really appreciate If you could help me to sort this problem out.

My method looks like this :

private void PrintDataTable(string StiTableName, string DataSourceName, DataTable dataTable, StiReport report)
{

    DataView dataView = new DataView(dataTable);

    report.Compile();

    //script lang
    report.ScriptLanguage = StiReportLanguageType.CSharp;

    // Add data to datastore
    report.RegData(DataSourceName, dataView);

    // Fill dictionary
    report.Dictionary.Synchronize();

    //StiPage page = report.Pages.Items[0];

    // Create Table
    StiTable table = (StiTable)report[StiTableName];
    //StiTable table = (StiTable)report.GetComponentByName(StiTableName);
    table.DataSourceName = DataSourceName;
    table.AutoWidthType = StiTableAutoWidthType.LastColumns;
    table.ColumnCount = dataTable.Columns.Count;
    table.RowCount = 3;
    table.HeaderRowsCount = 1;
    table.FooterRowsCount = 1;
    //table.Width = page.Width;
    //table.Height = page.GridSize * 12;
    //table.DataSourceName = DataSourceName;
    table.CreateCell();
    table.TableStyleFX = new StiTable21StyleFX();
    table.TableStyle = Stimulsoft.Report.Components.Table.StiTableStyle.Style59;


    int indexHeaderCell = 0;
    int indexDataCell = dataTable.Columns.Count;
    //int indexDataCell = dataTable.Columns.Count;

    foreach (DataColumn column in dataView.Table.Columns)
    {
        // Set text on header
        StiTableCell headerCell = table.Components[indexHeaderCell] as StiTableCell;
        headerCell.Text.Value = column.Caption;
        headerCell.HorAlignment = StiTextHorAlignment.Center;
        headerCell.VertAlignment = StiVertAlignment.Center;


        StiTableCell dataCell = table.Components[indexDataCell] as StiTableCell;
        dataCell.HorAlignment = StiTextHorAlignment.Center;
        headerCell.VertAlignment = StiVertAlignment.Center;
        dataCell.Text.Value = "{" + DataSourceName + "." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName) + "}";
        dataCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(32, 178, 170), 1, StiPenStyle.Dash);
              
        indexHeaderCell++;
        indexDataCell++;
    }

    // Set text on footer
    StiTableCell footerCell = table.Components[table.Components.Count - 1] as StiTableCell;
    footerCell.Text.Value = "Count - {Count()}";
    footerCell.Font = new Font("Arial", 15, FontStyle.Bold);
    footerCell.VertAlignment = StiVertAlignment.Center;
    footerCell.HorAlignment = StiTextHorAlignment.Center;

}

Thank you.

I'ts been 2 weeks and I even tried other ways like

report.GetComponentByName("Table1");

and still nothing , I really need this in short time , Will appreiciate your help. Thank you.


Solution

  • This is a HOTFIX:

    private void PrintTableFine(StiReport report ,DataTable dataTable)
            {
                DataView dataView = new DataView(dataTable);
    
                report.Load(Application.StartupPath + "\\A4 Portrait.mrt");
    
                report.ScriptLanguage = StiReportLanguageType.CSharp;
    
                // Add data to datastore
                report.RegData("view", dataView);
    
                // Fill dictionary
                report.Dictionary.Synchronize();
    
                StiPage page = report.Pages.Items[0];
    
                // Create Table
                StiTable table = new StiTable();
                table.Name = "Table1";
                table.AutoWidthType = StiTableAutoWidthType.LastColumns;
                table.ColumnCount = dataTable.Columns.Count;
                table.RowCount = 3;
                table.HeaderRowsCount = 1;
                table.FooterRowsCount = 1;
                table.Width = page.Width;
                table.Height = page.GridSize * 12;
                table.DataSourceName = "view" + dataView.Table.TableName;
                page.Components.Add(table);
                table.CreateCell();
                table.TableStyleFX = new StiTable21StyleFX();
                table.TableStyle = Stimulsoft.Report.Components.Table.StiTableStyle.Style31;
    
                int indexHeaderCell = 0;
                int indexDataCell = dataTable.Columns.Count;
    
                foreach (DataColumn column in dataView.Table.Columns)
                {
                    // Set text on header
                    StiTableCell headerCell = table.Components[indexHeaderCell] as StiTableCell;
                    headerCell.Text.Value = column.Caption;
                    headerCell.Font = new Font("IRANSans(FaNum)", 10, FontStyle.Bold);
                    headerCell.HorAlignment = StiTextHorAlignment.Center;
                    headerCell.VertAlignment = StiVertAlignment.Center;
                    headerCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(0, 0, 0), 1, StiPenStyle.Dash);
    
                    StiTableCell dataCell = table.Components[indexDataCell] as StiTableCell;
                    dataCell.Text.Value = "{view" + dataView.Table.TableName + "." +
                        Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName) + "}";
                    dataCell.Font = new Font("IRANSans(FaNum)", 10, FontStyle.Regular);
                    dataCell.HorAlignment = StiTextHorAlignment.Center;
                    dataCell.VertAlignment = StiVertAlignment.Center;
                    dataCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(0, 0, 0), 1, StiPenStyle.Dash);
    
                    indexHeaderCell++;
                    indexDataCell++;
                }
    
                // Set text on footer
                StiTableCell footerCell = table.Components[table.Components.Count - 1] as StiTableCell;
                //footerCell.Text.Value = "Count - {Count()}";
                footerCell.Font = new Font("IRANSans(FaNum)", 10, FontStyle.Regular);
                footerCell.VertAlignment = StiVertAlignment.Center;
                footerCell.HorAlignment = StiTextHorAlignment.Center;
            }
    

    Actually it creates a table. Please remember , to use it like this :

    DataTable dt = new DataTable(); 
    
                StiReport report = new StiReport();
    
                //Reminder: compiling is after the table 
    
                //table: 
                PrintTableFine(report,dt);
    
                report.Compile();
    
                //adding a variable:
                report["Variable"] = "var1ا";
               
                report.Render(false);
                report.Show();
    

    Just give it a DataTable and it will create a table with header and footer. Reminder: the report which your adding this table to SHOULD NOT HAVE A (Sti)TABLE IN IT(in the design).