Search This Blog

Thursday, October 1, 2015

Telerik report bind to DataTable and add a sample calculated field

Telerik > Reporting > Bind to DataTable

Example
Bind to DataTable and add a sample calculated field.

string connString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=HR;Integrated Security=True";
string cmdText = "SELECT FirstName, LastName FROM Person;";

SqlDataAdapter da = new SqlDataAdapter(cmdText, connString);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);

Telerik.Reporting.ObjectDataSource ods = new Telerik.Reporting.ObjectDataSource();
ods.DataSource = dt;
ods.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.FirstName + ' ' + Fields. LastName ")); 
Telerik.Reporting.Report rpt = new Telerik.Reporting.Report();
rpt.DataSource = objectDataSource;
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = rpt;
reportViewer1.ReportSource = reportSource;
reportViewer1.RefreshReport();