This question is locked. New answers and comments are not allowed.
HI
visible false column exporting in my application.
if i have 3 columns in my RadGridView, how do i export only the 2 columns?
I only want to visible false the column for export purpose only
Here i attached my code.
Print extension class is.
I am waiting for your reply.
thank you
Amit
visible false column exporting in my application.
if i have 3 columns in my RadGridView, how do i export only the 2 columns?
I only want to visible false the column for export purpose only
Here i attached my code.
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Collections.ObjectModel;using System.Xml.Linq;namespace RadGridViewPrint{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); DataContext = new MyDataContext(); } private void Button_Click(object sender, RoutedEventArgs e) { PrintExportExtensions ex = new PrintExportExtensions(); ex.Print(RadGridView1); } private void Button2_Click(object sender, RoutedEventArgs e) { PrintExportExtensions ex = new PrintExportExtensions(); var hiddenColumns = new[] { "CustomerID" }; foreach (var columnName in hiddenColumns) { this.RadGridView1.Columns[columnName].IsVisible = false; } ex.ExportToExcel(RadGridView1); foreach (var columnName in hiddenColumns) { this.RadGridView1.Columns[columnName].IsVisible = true; } } private void Button3_Click(object sender, RoutedEventArgs e) { PrintExportExtensions ex = new PrintExportExtensions(); ex.Export(RadGridView1); } } public class MyDataContext { ObservableCollection<Customer> _Customers; public ObservableCollection<Customer> Customers { get { if (_Customers == null) { _Customers = new ObservableCollection<Customer>(); var document = XDocument.Load("Customers.xml"); foreach (var element in document.Descendants("Customers")) { var customer = new Customer(); customer.CustomerID = element.Element("CustomerID").Value; customer.CompanyName = element.Element("CompanyName").Value; customer.Country = element.Element("Country").Value; customer.City = element.Element("City").Value; customer.ContactName = element.Element("ContactName").Value; customer.ContactTitle = element.Element("ContactTitle").Value; customer.Address = element.Element("Address").Value; customer.PostalCode = element.Element("PostalCode") != null ? element.Element("PostalCode").Value : null; customer.Phone = element.Element("Phone").Value; customer.Fax = element.Element("Fax") != null ? element.Element("Fax").Value : null; customer.Bool = (bool)element.Element("Bool"); _Customers.Add(customer); } } return _Customers; } } }}Print extension class is.
using System.Collections.Generic;using System.Linq;using Telerik.Windows.Controls;using System.Windows.Media;using Telerik.Windows.Data;using Telerik.Windows.Documents.Model;using System.Collections;using Telerik.Windows.Documents.FormatProviders.Pdf;using Telerik.Windows.Documents.FormatProviders.Html;using System.IO;using System.ComponentModel;using System.Windows;using System;using Microsoft.Win32;using System.Windows.Controls;using Telerik.Windows.Documents.FormatProviders;using Telerik.Windows.Controls.GridView;namespace RadGridViewPrint{ public class PrintExportExtensions : DependencyObject, INotifyPropertyChanged { public PrintExportExtensions() { //DocumentFormatProvidersManager.RegisterFormatProvider(new HtmlFormatProvider()); this.HeaderBackground = Color.FromArgb(255, 127, 127, 127); this.RowBackground = Color.FromArgb(255, 251, 247, 255); this.GroupHeaderBackground = Color.FromArgb(255, 216, 216, 216); } private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private Color _headerBackground; private Color _rowBackground; private Color _groupHeaderBackground; public Color GroupHeaderBackground { get { return this._groupHeaderBackground; } set { if (this._groupHeaderBackground != value) { this._groupHeaderBackground = value; OnPropertyChanged("GroupHeaderBackground"); } } } public Color HeaderBackground { get { return this._headerBackground; } set { if (this._headerBackground != value) { this._headerBackground = value; OnPropertyChanged("HeaderBackground"); } } } public Color RowBackground { get { return this._rowBackground; } set { if (this._rowBackground != value) { this._rowBackground = value; OnPropertyChanged("RowBackground"); } } } public void Export(object parameter) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "*.pdf"; dialog.Filter = "Adobe PDF Document (*.pdf)|*.pdf"; if (dialog.ShowDialog() == true) { RadDocument document = CreateDocument(parameter as RadGridView); document.LayoutMode = DocumentLayoutMode.Paged; document.Measure(RadDocument.MAX_DOCUMENT_SIZE); document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); document.SectionDefaultPageOrientation = PageOrientation.Landscape; Telerik.Windows.Documents.Layout.Padding padding = new Telerik.Windows.Documents.Layout.Padding(20); document.SectionDefaultPageMargin = padding; document.SectionDefaultPageSize = new Size(826, 1160); PdfFormatProvider provider = new PdfFormatProvider(); using (Stream output = dialog.OpenFile()) { provider.Export(document, output); } } } public void ExportToExcel(object parameter) { try { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "*.xls"; dialog.Filter = "Adobe PDF Document (*.xls)|*.xls"; if (dialog.ShowDialog() == true) { RadDocument document = CreateDocument(parameter as RadGridView); document.LayoutMode = DocumentLayoutMode.Paged; document.Measure(RadDocument.MAX_DOCUMENT_SIZE); document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); document.SectionDefaultPageOrientation = PageOrientation.Landscape; Telerik.Windows.Documents.Layout.Padding padding = new Telerik.Windows.Documents.Layout.Padding(20); document.SectionDefaultPageMargin = padding; document.SectionDefaultPageSize = new Size(826, 1160); HtmlFormatProvider provider = new HtmlFormatProvider(); using (Stream output = dialog.OpenFile()) { provider.Export(document, output); } } } catch (Exception ex) { throw ex; } } bool isPrintStarted = false; //bool isExportStarted = false; public void Print(object parameter) { RadGridView grid = (RadGridView)parameter; RadRichTextBox rtb = new RadRichTextBox() { Height = 0 }; rtb.Name = "RadRichTextBox1"; Grid parent = grid.ParentOfType<Grid>(); if (parent != null && parent.FindName(rtb.Name) == null) { parent.Children.Add(rtb); rtb.ApplyTemplate(); } rtb.Dispatcher.BeginInvoke((Action)(() => { rtb.Document = CreateDocument(grid); })); RadWindow window = new RadWindow(); Grid g = new Grid(); //g.ColumnDefinitions.Add(new ColumnDefinition()); g.ColumnDefinitions.Add(new ColumnDefinition()); g.RowDefinitions.Add(new RowDefinition()); g.HorizontalAlignment = HorizontalAlignment.Stretch; g.VerticalAlignment = VerticalAlignment.Stretch; Button b = new Button(); b.Height = 50; b.Width = 100; b.Content = "Print"; b.Click += (s, e) => { rtb.Commands.ChangePageOrientationCommand.Execute(PageOrientation.Landscape); rtb.ChangeSectionPageOrientation(PageOrientation.Landscape); rtb.Document.SectionDefaultPageOrientation = PageOrientation.Landscape; Telerik.Windows.Documents.Layout.Padding padding = new Telerik.Windows.Documents.Layout.Padding(20); rtb.Commands.ChangePageMarginsCommand.Execute(padding); rtb.Document.SectionDefaultPageMargin = padding; rtb.ChangeSectionPageMargin(padding); rtb.ChangeSectionPageSize(new Size(826, 1160)); rtb.Document.SectionDefaultPageSize = new Size(826, 1160); rtb.ShowComments = false; rtb.Commands.ChangeSectionFooterBottomMarginCommand.Execute(0); rtb.ChangeSectionFooterBottomMargin(0); rtb.Print("MyDocument", Telerik.Windows.Documents.UI.PrintMode.Native); isPrintStarted = true; window.Close(); }; rtb.PrintCompleted += (s, e) => { parent.Children.Remove(rtb); isPrintStarted = false; }; window.Closed += (s, e) => { if (!isPrintStarted) { parent.Children.Remove(rtb); } else { //wait print complete to remove the children ; } }; g.Children.Add(b); Grid.SetRow(b, 0); Grid.SetColumn(b, 0); //Button b2 = new Button(); //b2.Content = "Preview"; //b2.Click += (s, e) => //{ // isExportStarted = true; // Export(grid); // window.Close(); //}; //grid.ElementExported += (s, e) => // { // parent.Children.Remove(rtb); // isExportStarted = false; // }; //g.Children.Add(b2); //Grid.SetRow(b2, 0); //Grid.SetColumn(b2, 1); window.Content = g; window.Height = 110; window.Width = 250; window.Header = "Print"; window.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen; window.ShowDialog(); } private RadDocument CreateDocument(RadGridView grid) { List<GridViewBoundColumnBase> columns = (from c in grid.Columns.OfType<GridViewBoundColumnBase>() orderby c.DisplayIndex select c).ToList(); Telerik.Windows.Documents.Model.Table table = new Telerik.Windows.Documents.Model.Table(); RadDocument document = new RadDocument(); Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section(); section.Blocks.Add(table); document.Sections.Add(section); if (grid.ShowColumnHeaders) { Telerik.Windows.Documents.Model.TableRow headerRow = new Telerik.Windows.Documents.Model.TableRow(); if (grid.GroupDescriptors.Count > 0) { Telerik.Windows.Documents.Model.TableCell indentCell = new Telerik.Windows.Documents.Model.TableCell(); indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count * 20); indentCell.Background = HeaderBackground; headerRow.Cells.Add(indentCell); } for (int i = 0; i < columns.Count; i++) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); cell.Background = HeaderBackground; if (columns[i].Header is string) AddCellValue(cell, columns[i].Header.ToString()); else AddCellValue(cell, columns[i].UniqueName); //cell.PreferredWidth = new TableWidthUnit((float)columns[i].ActualWidth); TableWidthUnit t = new TableWidthUnit(TableWidthUnitType.Auto); cell.PreferredWidth = t; headerRow.Cells.Add(cell); } table.Rows.Add(headerRow); } if (grid.Items.Groups != null) { for (int i = 0; i < grid.Items.Groups.Count; i++) { AddGroupRow(table, grid.Items.Groups[i] as QueryableCollectionViewGroup, columns, grid); } } else { AddDataRows(table, grid.Items, columns, grid); } foreach (GridViewBoundColumnBase b in columns) { TableWidthUnit unit = new TableWidthUnit(TableWidthUnitType.Auto); table.SetGridColumnWidth(b.DisplayIndex, unit); } return document; } private void AddDataRows(Telerik.Windows.Documents.Model.Table table, IList items, IList<GridViewBoundColumnBase> columns, RadGridView grid) { for (int i = 0; i < items.Count; i++) { Telerik.Windows.Documents.Model.TableRow row = new Telerik.Windows.Documents.Model.TableRow(); if (grid.GroupDescriptors.Count > 0) { Telerik.Windows.Documents.Model.TableCell indentCell = new Telerik.Windows.Documents.Model.TableCell(); indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count * 20); indentCell.Background = RowBackground; row.Cells.Add(indentCell); } for (int j = 0; j < columns.Count; j++) { object value = columns[j].GetValueForItem(items[i]); if (value is string || value is int || value is double || value is double? || value is int?) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); AddCellValue(cell, value != null ? value.ToString() : string.Empty); cell.PreferredWidth = new TableWidthUnit((float)columns[j].ActualWidth); cell.Background = RowBackground; row.Cells.Add(cell); } else { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); AddCellValue(cell, value != null ? "/" : string.Empty); cell.PreferredWidth = new TableWidthUnit((float)columns[j].ActualWidth); cell.Background = RowBackground; row.Cells.Add(cell); } } table.Rows.Add(row); } } private void AddGroupRow(Telerik.Windows.Documents.Model.Table table, QueryableCollectionViewGroup group, IList<GridViewBoundColumnBase> columns, RadGridView grid) { Telerik.Windows.Documents.Model.TableRow row = new Telerik.Windows.Documents.Model.TableRow(); int level = GetGroupLevel(group); if (level > 0) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); cell.PreferredWidth = new TableWidthUnit(level * 20); cell.Background = GroupHeaderBackground; row.Cells.Add(cell); } Telerik.Windows.Documents.Model.TableCell aggregatesCell = new Telerik.Windows.Documents.Model.TableCell(); aggregatesCell.Background = GroupHeaderBackground; aggregatesCell.ColumnSpan = columns.Count + (grid.GroupDescriptors.Count > 0 ? 1 : 0) - (level > 0 ? 1 : 0); AddCellValue(aggregatesCell, group.Key != null ? group.Key.ToString() : string.Empty); foreach (AggregateResult result in group.AggregateResults) { AddCellValue(aggregatesCell, result.FormattedValue != null ? result.FormattedValue.ToString() : string.Empty); } row.Cells.Add(aggregatesCell); table.Rows.Add(row); if (group.HasSubgroups) { foreach (var g in group.Subgroups) { AddGroupRow(table, g as QueryableCollectionViewGroup, columns, grid); } } else { AddDataRows(table, group.Items, columns, grid); } } private void AddCellValue(Telerik.Windows.Documents.Model.TableCell cell, string value) { Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph(); cell.Blocks.Add(paragraph); Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span(); if (value == null || value == string.Empty) value = "/"; span.Text = value; paragraph.Inlines.Add(span); } private int GetGroupLevel(IGroup group) { int level = 0; IGroup parent = group.ParentGroup; while (parent != null) { level++; parent = parent.ParentGroup; } return level; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion }}I am waiting for your reply.
thank you
Amit