Change telerik report table data at runtime

1 Answer 449 Views
Miscellaneous
Ali
Top achievements
Rank 1
Ali asked on 23 Jun 2022, 06:30 AM | edited on 23 Jun 2022, 06:32 AM

Hello every one ,

in this question i just want to create Telerik report with an empty table  and assign table data source from code.

in output window the Telerik Report Show without table

here is my code from window form  :

all this code below written in from load of reportviewerform.cs

var sourceReportSource = new UriReportSource { Uri = @"C:\Users\ali.raza\source\repos\telerick_windowform\telerick_windowform\Reports\SampleReport.trdp" };
            var reportInstance = UnpackageReport(sourceReportSource);
            var report = (Report)reportInstance;
            Telerik.Reporting.Table processingTable = report.Items.Find("table1", true)[0] as Telerik.Reporting.Table;
            //Telerik.Reporting.Processing.Table processingTable = (sender as Telerik.Reporting.Processing.Table);
            this.table2.ColumnGroups.Clear();
            this.table2.Body.Columns.Clear();
            this.table2.Body.Rows.Clear();
            Telerik.Reporting.TextBox textboxGroup;
            Telerik.Reporting.TextBox textBoxTable;
            string connectiostring = "server=win-dc;database=testdb;uid=hrs;password=hrs;Initial Catalog=testdb;Integrated Security= false";
            string selectstring = "select * from tbl_telerik";
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(selectstring, connectiostring);
            DataSet ds = new DataSet();
            sqlAdapter.Fill(ds);
            Telerik.Reporting.TableGroup tableGroupColumn;
            int i = 0;
            foreach (System.Data.DataColumn dc in ds.Tables[0].Columns)
            {
                tableGroupColumn = new Telerik.Reporting.TableGroup();

                this.table2.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));

                textboxGroup = new Telerik.Reporting.TextBox();
                textboxGroup.Value = dc.ColumnName.ToString();
                textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                tableGroupColumn.ReportItem = textboxGroup;
                table2.ColumnGroups.Add(tableGroupColumn);

                textBoxTable = new Telerik.Reporting.TextBox();
                table2.Body.SetCellContent(0, i++, textBoxTable);
                textBoxTable.Value = "=Fields." + dc.ColumnName;
            }
            Telerik.Reporting.TableGroup tableGroupRow = new Telerik.Reporting.TableGroup();
            this.table2.RowGroups.Add(tableGroupRow);
            tableGroupRow.Grouping.Add(new Telerik.Reporting.Grouping());//new Telerik.Reporting.Data.Grouping());
            processingTable.DataSource = ds.Tables[0];
            var reportSource = CreateInstanceReportSource(reportInstance, sourceReportSource);
            this.reportViewer1.ReportSource = reportSource;
            this.reportViewer1.RefreshReport();

Ali
Top achievements
Rank 1
commented on 28 Jun 2022, 12:25 PM

I have Almost Done  on what i am doing just need a little help 

this is my code form generating table programmatically.

string connectionString = "Data Source=;Initial Catalog=;Integrated Security=False;User ID=;Password=";// no connection string error.

            //get the processing table object since we're in the context of event
            Telerik.Reporting.Processing.Table processingTable = (sender as Telerik.Reporting.Processing.Table);
            //construct the select statement based on the selected report parameters
            string selectString = "SELECT " + FormatArray((object[])processingTable.Report.Parameters["TableColumns"].Value) + " FROM " + processingTable.Report.Parameters["TableName"].Value;
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(selectString, connectionString);
            //create a dataset, fill it and set it as datasource to the processing table object
            DataSet ds = new DataSet();
            sqlAdapter.Fill(ds);
            processingTable.DataSource = ds.Tables[0];

            //create two HtmlTextBox items (one for header and one for data) which would be added to the items collection of the table
            Telerik.Reporting.HtmlTextBox textboxGroup;
            Telerik.Reporting.HtmlTextBox textBoxTable;

            //we do not clear the Rows collection, since we have a details row group and need to create columns only
            this.table1.ColumnGroups.Clear();
            this.table1.Body.Columns.Clear();
            this.table1.Body.Rows.Clear();
            int i = 0;
            this.table1.ColumnHeadersPrintOnEveryPage = true;
            foreach (System.Data.DataColumn dc in ds.Tables[0].Columns)
            {
                Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
                this.table1.ColumnGroups.Add(tableGroupColumn);
                this.table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));

                textboxGroup = new Telerik.Reporting.HtmlTextBox();
                textboxGroup.Style.BorderColor.Default = Color.Black;
                textboxGroup.Style.BackgroundColor = Color.AliceBlue;
                textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
                textboxGroup.Value = dc.ColumnName.ToString();
                textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                tableGroupColumn.ReportItem = textboxGroup;

                textBoxTable = new Telerik.Reporting.HtmlTextBox();
                textBoxTable.Style.BorderColor.Default = Color.Black;
                textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
                textBoxTable.Value = "=Fields." + dc.ColumnName.ToString();
                textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                this.table1.Body.SetCellContent(0, i++, textBoxTable);
                this.table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
            }

this code is generating table column perfect but repeat row data.

OUTPUT.

Neli
Telerik team
commented on 29 Jun 2022, 09:19 AM

Hi Ali,

Indeed, the outcome is strange. Can you please please send us a sample project with the AdventureWorks database which we distribute with the Examples installation, so we can test in our end?

In general, if the report structure is the same and you only need to change the connection string, I would recommend some of the following approaches.

One of the options is the approach described in the KB Change Connection String dynamically through a report parameter. Then from the viewer, you can pass the connection string. Note that this won't work for the connection string of Report Parameter AvailableValues DataSource if any.

The other one is:  Changing the connection string dynamically according to runtime data KB article is suitable for Desktop Viewers.

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 27 Jun 2022, 01:14 PM

Hello Ali,

If you need to create the tables dynamically, I would recommend checking the following resources:

How to Create a Dynamic Table/Crosstab/List item (contains a sample report)

How to add multiple tables dynamically based on the data source (contains a sample report)

- How to modify table with preset styles and width dynamically

Regards,
Neli
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Ali
Top achievements
Rank 1
commented on 28 Jun 2022, 12:18 PM

Thanks for the helping Hand. Neli

Tags
Miscellaneous
Asked by
Ali
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or