Posted 28 Apr 2014
Link to this post
Thanks Yoan,
I am glad to inform you that I am creating the code for create telerik report runtime using RadGridView :).
Print Code:
public static void PrintTelerikReport(RadGridView p_RadGridView)
{
try
{
ReportSource _ReportSource = GenerateReport(p_RadGridView);
ReportProcessor _ReportProcessor = new ReportProcessor();
_ReportProcessor.PrintReport(_ReportSource, null);
}
catch (Exception _Exception)
{
MessageBox.Show(_Exception.ToString());
}
}
Print Preview Code :
void PrintPreviewTelerikReportViewer_Loaded(object sender, RoutedEventArgs e)
{
try
{
rvCommonReportViewer.ReportSource = GenerateReport(_CurrentRadGridView);
rvCommonReportViewer.RefreshReport();
}
catch (Exception _Exception)
{
MessageBox.Show(_Exception.ToString());
}
}
Common static Method :
private static Telerik.Reporting.Report GenerateReport(RadGridView p_RadGridView)
{
#region Variables For Report
Telerik.Reporting.Report _testReport = new Telerik.Reporting.Report();
Telerik.Reporting.GroupHeaderSection _GroupHeaderSection = new Telerik.Reporting.GroupHeaderSection();
Telerik.Reporting.GroupFooterSection _GroupFooterSection = new Telerik.Reporting.GroupFooterSection();
Telerik.Reporting.PageFooterSection _PageFooterSection = new Telerik.Reporting.PageFooterSection();
Telerik.Reporting.TextBox _CurrentTimeTextBox = new Telerik.Reporting.TextBox();
Telerik.Reporting.TextBox _PageInfoTextBox = new Telerik.Reporting.TextBox();
Telerik.Reporting.ReportHeaderSection _ReportHeaderSection = new Telerik.Reporting.ReportHeaderSection();
Telerik.Reporting.TextBox _TitleTextBox = new Telerik.Reporting.TextBox();
Telerik.Reporting.DetailSection _DetailSection = new Telerik.Reporting.DetailSection();
Telerik.Reporting.Group _Group = new Telerik.Reporting.Group();
Telerik.Reporting.Drawing.StyleRule _StyleRule1 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.StyleRule _StyleRule2 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.StyleRule _StyleRule3 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.StyleRule _StyleRule4 = new Telerik.Reporting.Drawing.StyleRule();
double _location = 0.02;
#endregion
#region This is for Detail
foreach (Telerik.Windows.Controls.GridViewColumn _GridViewColumn in p_RadGridView.Columns)
{
if (_GridViewColumn.GetType() != typeof(GridViewSelectColumn))
{
double _DataWidth = Convert.ToDouble(_GridViewColumn.Width.Value / 100);
Telerik.Reporting.TextBox _tbHeader = new Telerik.Reporting.TextBox();
_tbHeader.CanGrow = true;
_tbHeader.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_location), Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D));
_tbHeader.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(_DataWidth), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));
_tbHeader.Style.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_tbHeader.Style.Font.Bold = true;
_tbHeader.Style.Font.Name = "Segoe UI";
_tbHeader.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);
_tbHeader.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Center;
_tbHeader.StyleName = "Caption";
_tbHeader.Value = _GridViewColumn.Header.ToString();
_GroupHeaderSection.Items.Add(_tbHeader);
Telerik.Reporting.TextBox _tbData = new Telerik.Reporting.TextBox();
_tbData.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_location), Telerik.Reporting.Drawing.Unit.Inch(3.9577484130859375E-05D));
_tbData.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(_DataWidth), Telerik.Reporting.Drawing.Unit.Inch(0.20));
_tbData.Style.Font.Name = "Segoe UI";
_tbData.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);
_tbData.Value = "=Fields." + _GridViewColumn.UniqueName.SafeTrim();
_DetailSection.Items.Add(_tbData);
_location += Convert.ToDouble(_GridViewColumn.Width.Value / 100);
}
}
#endregion
#region This is for Page Footer
// labelsGroupFooterSection
_GroupFooterSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.099999904632568359D);
_GroupFooterSection.Name = "labelsGroupFooterSection";
_GroupFooterSection.Style.Visible = false;
// pageFooter
_PageFooterSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.44166669249534607D);
_PageFooterSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {_CurrentTimeTextBox, _PageInfoTextBox});
_PageFooterSection.Name = "pageFooter";
// currentTimeTextBox
_CurrentTimeTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D), Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D));
_CurrentTimeTextBox.Name = "currentTimeTextBox";
_CurrentTimeTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(3.1979167461395264D), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));
_CurrentTimeTextBox.Style.Font.Name = "Segoe UI";
_CurrentTimeTextBox.StyleName = "PageInfo";
_CurrentTimeTextBox.Value = "=NOW()";
// pageInfoTextBox
_PageInfoTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(3.2395832538604736D), Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D));
_PageInfoTextBox.Name = "pageInfoTextBox";
_PageInfoTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(3.1979167461395264D), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));
_PageInfoTextBox.Style.Font.Name = "Segoe UI";
_PageInfoTextBox.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Right;
_PageInfoTextBox.StyleName = "PageInfo";
_PageInfoTextBox.Value = "=PageNumber";
#endregion
#region This is for Page Header
// labelsGroupHeaderSection
_GroupHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.44166669249534607D);
_GroupHeaderSection.Name = "labelsGroupHeaderSection";
_GroupHeaderSection.PrintOnEveryPage = true;
// reportHeader
_ReportHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.35833343863487244D);
_ReportHeaderSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
_TitleTextBox});
_ReportHeaderSection.Name = "reportHeader";
// titleTextBox
_TitleTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.20000000298023224D), Telerik.Reporting.Drawing.Unit.Inch(0D));
_TitleTextBox.Name = "titleTextBox";
_TitleTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(6.2375001907348633D), Telerik.Reporting.Drawing.Unit.Inch(0.31999999284744263D));
_TitleTextBox.Style.Color = System.Drawing.Color.Black;
_TitleTextBox.Style.Font.Name = "Segoe UI";
_TitleTextBox.StyleName = "Title";
_TitleTextBox.Value = "Test Summary Report";
#endregion
// detail
_DetailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.20);
_DetailSection.Name = "detail";
_Group.GroupFooter = _GroupFooterSection;
_Group.GroupHeader = _GroupHeaderSection;
_testReport.Groups.AddRange(new Telerik.Reporting.Group[] {_Group});
_testReport.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {_GroupHeaderSection, _GroupFooterSection, _PageFooterSection, _ReportHeaderSection, _DetailSection});
#region This is for Style
_testReport.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D));
_testReport.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;
_testReport.Style.BackgroundColor = System.Drawing.Color.White;
_testReport.Style.BorderColor.Bottom = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_testReport.Style.BorderColor.Default = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_testReport.Style.BorderColor.Left = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_testReport.Style.BorderColor.Right = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_testReport.Style.BorderColor.Top = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_testReport.Style.BorderStyle.Bottom = Telerik.Reporting.Drawing.BorderType.Solid;
_testReport.Style.BorderStyle.Left = Telerik.Reporting.Drawing.BorderType.Solid;
_testReport.Style.BorderStyle.Right = Telerik.Reporting.Drawing.BorderType.Solid;
_testReport.Style.BorderStyle.Top = Telerik.Reporting.Drawing.BorderType.Solid;
_testReport.Style.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));
_StyleRule1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.StyleSelector("Title")});
_StyleRule1.Style.Color = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));
_StyleRule1.Style.Font.Name = "Tahoma";
_StyleRule1.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(18D);
_StyleRule2.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.StyleSelector("Caption")});
_StyleRule2.Style.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));
_StyleRule2.Style.Color = System.Drawing.Color.White;
_StyleRule2.Style.Font.Name = "Tahoma";
_StyleRule2.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(10D);
_StyleRule2.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
_StyleRule3.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.StyleSelector("Data")});
_StyleRule3.Style.Color = System.Drawing.Color.Black;
_StyleRule3.Style.Font.Name = "Tahoma";
_StyleRule3.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(9D);
_StyleRule3.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
_StyleRule4.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.StyleSelector("PageInfo")});
_StyleRule4.Style.Color = System.Drawing.Color.Black;
_StyleRule4.Style.Font.Name = "Tahoma";
_StyleRule4.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);
_StyleRule4.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
_testReport.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] {
_StyleRule1,
_StyleRule2,
_StyleRule3,
_StyleRule4});
_testReport.Width = Telerik.Reporting.Drawing.Unit.Inch(6.4375D);
#endregion
_testReport.DataSource = p_RadGridView.ItemsSource;
return _testReport;
}
Thank You,
Yogesh Goswami