This is a migrated thread and some comments may be shown as answers.

RadGridView Print and Print Preview using Telerik Reproting

1 Answer 69 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yogesh
Top achievements
Rank 1
Yogesh asked on 07 May 2014, 06:45 AM
Using following code, we can print & preview of RadGridView using Telerik Reproting with grouping, filtering functionality of RadGridView with nos. of columns are generated dynamic in the Report.

Code:

using System;<br>using System.Collections.Generic;<br>using System.Linq;<br>using System.Windows;<br>using Telerik.Reporting;<br>using System.Collections.ObjectModel;<br>using Telerik.Reporting.Processing;<br>using Telerik.Windows.Controls;<br>using Telerik.Windows.Controls.GridView;<br><br>namespace YourNamespaceHere<br>{<br>    /// <summary><br>    /// Interaction logic for PrintPreviewTelerikReportViewer.xaml<br>    /// </summary><br>    public partial class PrintPreviewTelerikReportViewer : Window<br>    {<br>        #region Variables<br>        RadGridView _CurrentRadGridView = new RadGridView();<br>        #endregion<br><br>        #region Ctor<br>        /// <summary><br>        /// ctor<br>        /// </summary><br>        /// <param name="p_RadGridView"></param><br>        public PrintPreviewTelerikReportViewer(RadGridView p_RadGridView)//this is for print preview<br>        {<br>            try<br>            {<br>                InitializeComponent();<br>                TitleBackground = Params.RibbonThemeSolidColor;<br>                _CurrentRadGridView = p_RadGridView;<br>                this.Loaded += new RoutedEventHandler(PrintPreviewTelerikReportViewer_Loaded);<br>            }<br>            catch (Exception _Exception)<br>            {<br>                MessageBox.Show(_Exception.Message.ToString());<br>            }<br>        }<br>        #endregion<br><br>        #region Events<br>        /// <summary><br>        /// loaded event<br>        /// </summary><br>        /// <param name="sender"></param><br>        /// <param name="e"></param><br>        void PrintPreviewTelerikReportViewer_Loaded(object sender, RoutedEventArgs e)<br>        {<br>            try<br>            {<br>                rvCommonReportViewer.ReportSource = GenerateReport(_CurrentRadGridView);<br>                rvCommonReportViewer.RefreshReport();<br>                rvCommonReportViewer.ViewMode = Telerik.ReportViewer.Wpf.ViewMode.PrintPreview;<br>            }<br>            catch (Exception _Exception)<br>            {<br>                MessageBox.Show(_Exception.Message.ToString());<br>            }<br>        }<br>        #endregion<br><br>        #region Methods<br>        /// <summary><br>        /// print telerik report static method<br>        /// </summary><br>        /// <param name="p_RadGridView"></param><br>        public static void PrintTelerikReport(RadGridView p_RadGridView)<br>        {<br>            try<br>            {<br>                ReportSource _ReportSource = GenerateReport(p_RadGridView);<br>                ReportProcessor _ReportProcessor = new ReportProcessor();<br>                _ReportProcessor.PrintReport(_ReportSource, null);<br>            }<br>            catch (Exception _Exception)<br>            {<br>                MessageBox.Show(_Exception.Message.ToString());<br>            }<br>        }<br><br>        /// <summary><br>        /// generate report static Methods<br>        /// </summary><br>        /// <param name="p_RadGridView"></param><br>        /// <returns></returns><br>        private static Telerik.Reporting.Report GenerateReport(RadGridView p_RadGridView)<br>        {<br>            #region Variables For Report<br><br>            Telerik.Reporting.Report _testReport = new Telerik.Reporting.Report();<br><br>            Telerik.Reporting.GroupHeaderSection _GroupHeaderSection = new Telerik.Reporting.GroupHeaderSection();<br>            Telerik.Reporting.GroupFooterSection _GroupFooterSection = new Telerik.Reporting.GroupFooterSection();<br>            Telerik.Reporting.PageFooterSection _PageFooterSection = new Telerik.Reporting.PageFooterSection();<br>            Telerik.Reporting.TextBox _CurrentTimeTextBox = new Telerik.Reporting.TextBox();<br>            Telerik.Reporting.TextBox _PageInfoTextBox = new Telerik.Reporting.TextBox();<br>            Telerik.Reporting.ReportHeaderSection _ReportHeaderSection = new Telerik.Reporting.ReportHeaderSection();<br>            Telerik.Reporting.TextBox _TitleTextBox = new Telerik.Reporting.TextBox();<br>            Telerik.Reporting.DetailSection _DetailSection = new Telerik.Reporting.DetailSection();<br><br>            Telerik.Reporting.Group _Group = new Telerik.Reporting.Group();<br><br>            Telerik.Reporting.Drawing.StyleRule _StyleRule1 = new Telerik.Reporting.Drawing.StyleRule();<br>            Telerik.Reporting.Drawing.StyleRule _StyleRule2 = new Telerik.Reporting.Drawing.StyleRule();<br>            Telerik.Reporting.Drawing.StyleRule _StyleRule3 = new Telerik.Reporting.Drawing.StyleRule();<br>            Telerik.Reporting.Drawing.StyleRule _StyleRule4 = new Telerik.Reporting.Drawing.StyleRule();<br><br>            double _Headerlocation = 0.02;<br>            double _GroupHeaderlocationY = 0.02;<br>            double _GroupHeaderlocationX = 0.02;<br>            #endregion<br><br>            #region This is for Detail<br>            //this is for group detail<br>            foreach (ColumnGroupDescriptor _ColumnGroupDescriptor in p_RadGridView.GroupDescriptors)<br>            {<br>                _Group.Groupings.Add(new Telerik.Reporting.Grouping("=Fields." + _ColumnGroupDescriptor.Column.UniqueName));<br><br>                Telerik.Reporting.TextBox _tbHeader = new Telerik.Reporting.TextBox();<br>                _tbHeader.CanGrow = true;<br>                _tbHeader.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_GroupHeaderlocationX), Telerik.Reporting.Drawing.Unit.Inch(_GroupHeaderlocationY));<br>                _tbHeader.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(5.0), Telerik.Reporting.Drawing.Unit.Inch(0.30000000596046448D));<br>                _tbHeader.Style.Font.Bold = true;<br>                _tbHeader.Style.Font.Name = "Segoe UI";<br>                _tbHeader.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);<br>                _tbHeader.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Left;<br>                _tbHeader.Value = "=Fields." + _ColumnGroupDescriptor.Column.UniqueName;<br><br>                _GroupHeaderSection.Items.Add(_tbHeader);<br><br>                _GroupHeaderlocationY += 0.20;<br>                _GroupHeaderlocationX += 0.10;<br>            }<br><br>            //this is for simple detail<br>            foreach (Telerik.Windows.Controls.GridViewColumn _GridViewColumn in p_RadGridView.Columns)<br>            {<br>                if (_GridViewColumn.GetType() != typeof(GridViewSelectColumn) && _GridViewColumn.IsVisible != false)<br>                {<br>                    double _DataWidth = Convert.ToDouble(_GridViewColumn.Width.Value / 100);<br><br>                    Telerik.Reporting.TextBox _tbHeader = new Telerik.Reporting.TextBox();<br>                    _tbHeader.CanGrow = true;<br>                    if (p_RadGridView.GroupDescriptors.Count != 0)<br>                    {<br>                        _tbHeader.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_Headerlocation), Telerik.Reporting.Drawing.Unit.Inch(_GroupHeaderlocationY));<br>                    }<br>                    else<br>                    {<br>                        _tbHeader.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_Headerlocation), Telerik.Reporting.Drawing.Unit.Inch(0.02));<br>                    }<br>                    _tbHeader.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(_DataWidth), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));<br>                    _tbHeader.Style.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>                    _tbHeader.Style.Font.Bold = true;<br>                    _tbHeader.Style.Font.Name = "Segoe UI";<br>                    _tbHeader.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);<br>                    _tbHeader.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Center;<br>                    _tbHeader.StyleName = "Caption";<br>                    _tbHeader.Value = _GridViewColumn.Header.ToString();<br><br>                    _GroupHeaderSection.Items.Add(_tbHeader);<br><br><br><br>                    Telerik.Reporting.TextBox _tbData = new Telerik.Reporting.TextBox();<br>                    _tbData.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(_Headerlocation), Telerik.Reporting.Drawing.Unit.Inch(3.9577484130859375E-05D));<br>                    _tbData.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(_DataWidth), Telerik.Reporting.Drawing.Unit.Inch(0.20));<br>                    _tbData.Style.Font.Name = "Segoe UI";<br>                    _tbData.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);<br>                    _tbData.Value = "=Fields." + _GridViewColumn.UniqueName;<br><br>                    _DetailSection.Items.Add(_tbData);<br><br>                    _Headerlocation += Convert.ToDouble(_GridViewColumn.Width.Value / 100);<br>                }<br>            }<br>            #endregion<br><br>            #region This is for Page Footer<br>            // labelsGroupFooterSection<br>            _GroupFooterSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.099999904632568359D);<br>            _GroupFooterSection.Name = "labelsGroupFooterSection";<br>            _GroupFooterSection.Style.Visible = false;<br><br>            // pageFooter<br>            _PageFooterSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.44166669249534607D);<br>            _PageFooterSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { _CurrentTimeTextBox, _PageInfoTextBox });<br>            _PageFooterSection.Name = "pageFooter";<br><br>            // currentTimeTextBox<br>            _CurrentTimeTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D), Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D));<br>            _CurrentTimeTextBox.Name = "currentTimeTextBox";<br>            _CurrentTimeTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(3.1979167461395264D), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));<br>            _CurrentTimeTextBox.Style.Font.Name = "Segoe UI";<br>            _CurrentTimeTextBox.StyleName = "PageInfo";<br>            _CurrentTimeTextBox.Value = "=NOW()";<br><br>            // pageInfoTextBox<br>            _PageInfoTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(3.2395832538604736D), Telerik.Reporting.Drawing.Unit.Inch(0.02083333395421505D));<br>            _PageInfoTextBox.Name = "pageInfoTextBox";<br>            _PageInfoTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(3.1979167461395264D), Telerik.Reporting.Drawing.Unit.Inch(0.40000000596046448D));<br>            _PageInfoTextBox.Style.Font.Name = "Segoe UI";<br>            _PageInfoTextBox.Style.TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Right;<br>            _PageInfoTextBox.StyleName = "PageInfo";<br>            _PageInfoTextBox.Value = "=PageNumber";<br>            #endregion<br><br>            #region This is for Page Header<br>            // labelsGroupHeaderSection<br>            _GroupHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.44166669249534607D);<br>            _GroupHeaderSection.Name = "labelsGroupHeaderSection";<br>            _GroupHeaderSection.PrintOnEveryPage = true;<br><br>            // reportHeader<br>            _ReportHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.35833343863487244D);<br>            _ReportHeaderSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {<br>            _TitleTextBox});<br>            _ReportHeaderSection.Name = "reportHeader";<br><br>            // titleTextBox<br>            _TitleTextBox.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.20000000298023224D), Telerik.Reporting.Drawing.Unit.Inch(0D));<br>            _TitleTextBox.Name = "titleTextBox";<br>            _TitleTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(6.2375001907348633D), Telerik.Reporting.Drawing.Unit.Inch(0.31999999284744263D));<br>            _TitleTextBox.Style.Color = System.Drawing.Color.Black;<br>            _TitleTextBox.Style.Font.Name = "Segoe UI";<br>            _TitleTextBox.StyleName = "Title";<br>            _TitleTextBox.Value = "Test Summary Report";<br>            #endregion<br><br>            // detail<br>            _DetailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.20);<br>            _DetailSection.Name = "detail";<br><br>            _Group.GroupFooter = _GroupFooterSection;<br>            _Group.GroupHeader = _GroupHeaderSection;<br><br>            _testReport.Groups.AddRange(new Telerik.Reporting.Group[] { _Group });<br>            _testReport.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { _GroupHeaderSection, _GroupFooterSection, _PageFooterSection, _ReportHeaderSection, _DetailSection });<br><br>            #region This is for Style<br>            _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));<br>            _testReport.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;<br>            _testReport.Style.BackgroundColor = System.Drawing.Color.White;<br>            _testReport.Style.BorderColor.Bottom = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _testReport.Style.BorderColor.Default = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _testReport.Style.BorderColor.Left = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _testReport.Style.BorderColor.Right = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _testReport.Style.BorderColor.Top = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _testReport.Style.BorderStyle.Bottom = Telerik.Reporting.Drawing.BorderType.Solid;<br>            _testReport.Style.BorderStyle.Left = Telerik.Reporting.Drawing.BorderType.Solid;<br>            _testReport.Style.BorderStyle.Right = Telerik.Reporting.Drawing.BorderType.Solid;<br>            _testReport.Style.BorderStyle.Top = Telerik.Reporting.Drawing.BorderType.Solid;<br>            _testReport.Style.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(37)))), ((int)(((byte)(160)))), ((int)(((byte)(218)))));<br>            _StyleRule1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {<br>            new Telerik.Reporting.Drawing.StyleSelector("Title")});<br>            _StyleRule1.Style.Color = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));<br>            _StyleRule1.Style.Font.Name = "Tahoma";<br>            _StyleRule1.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(18D);<br>            _StyleRule2.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {<br>            new Telerik.Reporting.Drawing.StyleSelector("Caption")});<br>            _StyleRule2.Style.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));<br>            _StyleRule2.Style.Color = System.Drawing.Color.White;<br>            _StyleRule2.Style.Font.Name = "Tahoma";<br>            _StyleRule2.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(10D);<br>            _StyleRule2.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;<br>            _StyleRule3.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {<br>            new Telerik.Reporting.Drawing.StyleSelector("Data")});<br>            _StyleRule3.Style.Color = System.Drawing.Color.Black;<br>            _StyleRule3.Style.Font.Name = "Tahoma";<br>            _StyleRule3.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(9D);<br>            _StyleRule3.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;<br>            _StyleRule4.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {<br>            new Telerik.Reporting.Drawing.StyleSelector("PageInfo")});<br>            _StyleRule4.Style.Color = System.Drawing.Color.Black;<br>            _StyleRule4.Style.Font.Name = "Tahoma";<br>            _StyleRule4.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(8D);<br>            _StyleRule4.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;<br>            _testReport.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] {<br>            _StyleRule1,<br>            _StyleRule2,<br>            _StyleRule3,<br>            _StyleRule4});<br>            _testReport.Width = Telerik.Reporting.Drawing.Unit.Inch(6.4375D);<br>            #endregion<br><br>            _testReport.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;<br><br>            //_testReport.DataSource = p_RadGridView.ItemsSource;//this is for all data<br>            _testReport.DataSource = p_RadGridView.Items;//this is for filter grid<br>            return _testReport;<br>        }<br>        #endregion<br>    }<br>}<br>


Thanks,
Yogesh Goswami

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 07 May 2014, 08:35 AM
Hello Yogesh,

Thank you for sharing your solution with the community. 
Let us know in case you have any questions related to RadGridView.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Yogesh
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or