Search...

Monday, March 18, 2013

Generate custom report in wpf


<Window x:Class="ListViewTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ListViewTutorial"
        Title="MainWindow"
        Height="380" 
        Width="650" 
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"
        Loaded="Window_Loaded">

    <Grid>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        
        <ListView Grid.Row="0" Grid.Column="0"
                  Name="LstSalesData" 
                  ScrollViewer.VerticalScrollBarVisibility="Auto" 
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectionMode="Multiple"
                  Height="300" 
                  VerticalAlignment="Top">
            
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="SNO." DisplayMemberBinding="{Binding Sno}" Width="50"/>
                    <GridViewColumn Header="Order Number" DisplayMemberBinding="{Binding OrderNumber}" Width="70"/>
                    <GridViewColumn Header="Order Date" DisplayMemberBinding="{Binding OrderDate,StringFormat=MMM dd yyyy}" Width="100"/>
                    <GridViewColumn Header="Customer Name" DisplayMemberBinding="{Binding CustomerName}" Width="100"/>
                    <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" Width="190"/>
                    <GridViewColumn Header="Warranty" DisplayMemberBinding="{Binding Warranty}" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Grid.Row="1" Grid.Column="0" 
                Name="btnPrint" 
                HorizontalAlignment="Right"
                Content="Print" 
                Click="Print_Click" 
                Width="100" 
                Margin="0,5,10,0"
                Height="25"/>
    </Grid>
</Window>

***************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace ListViewTutorial
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private List<SalesData> lstSalesData =null;
        private static readonly object locker = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow" /> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            this.lstSalesData = new List<SalesData>();
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.LstSalesData.ItemsSource = GetSalesData();
        }

        /// <summary>
        /// Gets the sales data.
        /// </summary>
        /// <returns></returns>
        private List<SalesData> GetSalesData()
        {
            try
            {
                Random randNum = new Random();

                String[] states={"Delhi","UP","MP","Ap","Panjap"};
                String[] names = { "Kaushik","Amit","Geeta","Naveen","Priyank","Rahul","Sumit","Pushp","Rohit","Ravi","Anand","Suresh","Rakesh","Ajay","Prabhat"};
                Boolean[] boolVals = { false, true, false, true, false, true, true, false };

                SalesData[] salesdata = new SalesData[101];
                for (int index = 1; index < salesdata.Length; index++)
                {
                    salesdata[index] = new SalesData();
                    salesdata[index].Sno = index.ToString();
                    salesdata[index].Address = String.Format("House Number  {0}, {1} ", randNum.Next(1, 1000), states[randNum.Next(0, states.Length - 1)].ToString());
                    salesdata[index].CustomerName = names[randNum.Next(0, 14)].ToString();
                    salesdata[index].OrderDate = DateTime.Now.AddDays(randNum.Next(1, 30));
                    salesdata[index].OrderNumber = randNum.Next(1, 1000);
                    salesdata[index].Warranty = boolVals[randNum.Next(0, boolVals.Length - 1)];
                    this.lstSalesData.Add(salesdata[index]);
                }
            }
            catch (Exception)
            { 
                throw;
            }
            return this.lstSalesData;
        }

        private void Print_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                PrintDialog printDialog = new PrintDialog();

                if (printDialog.ShowDialog() == false)
                    return;

                Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);

                GridDocumentPaginator paginator = new GridDocumentPaginator("123-06470", this.lstSalesData, pageSize, new Thickness(10, 10, 10, 10));
                printDialog.PrintDocument(paginator, "Sales Info");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

    /// <summary>
    /// Interaction logic for SalesData.
    /// </summary>
    internal class SalesData
    {
        public SalesData()
        { 
        
        }
        public String Sno { get; set; }
        public int OrderNumber { get; set; }
        public DateTime OrderDate { get; set; }
        public String CustomerName { get; set; }
        public String Address { get; set; }
        public Boolean Warranty { get; set; }
    }

    /// <summary>
    ///  This class contains interaction logic that supports creation of multiple-page elements from a single document.
    /// </summary>
    internal class GridDocumentPaginator : DocumentPaginator
    {
        #region Private Members

        private Double _avgRowHeight;

        private Double _availableHeight;

        private List<SalesData> _gridContentData = new List<SalesData>();

        private String _tinNumber = String.Empty;

        private int _rowsPerPage;

        private int _pageCount;

        private int _skipRows = 0;

        #endregion

        #region Constructor

        public GridDocumentPaginator(String tinNumber, List<SalesData> gridContentData, Size pageSize, Thickness pageMargin)
        {
            this.PageSize = pageSize;
            this.PageMargin = pageMargin;
            this._gridContentData = gridContentData;
            this._tinNumber = tinNumber;
            this._skipRows = 0;

            if (_gridContentData != null)
                MeasureElements();
        }

        #endregion

        #region Public Properties

        public Thickness PageMargin { get; set; }

        public override Size PageSize { get; set; }

        public override bool IsPageCountValid
        {
            get { return true; }
        }

        /// <summary>
        /// When overridden in a derived class, gets a count of the number of pages currently
        /// formatted
        /// A count of the number of pages that have been formatted.
        /// </summary>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        public override int PageCount
        {
            get { return _pageCount; }
        }

        /// <summary>
        /// When overridden in a derived class, returns the element being paginated.
        /// Returns:
        /// An System.Windows.Documents.IDocumentPaginatorSource representing the element
        /// being paginated.
        /// </summary>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        public override IDocumentPaginatorSource Source
        {
            get { return null; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// When overridden in a derived class, gets the System.Windows.Documents.DocumentPage
        /// for the specified page number.
        /// Parameters:
        /// pageNumber:
        /// The zero-based page number of the document page that is needed.
        /// Returns:
        /// The System.Windows.Documents.DocumentPage for the specified pageNumber, or
        /// System.Windows.Documents.DocumentPage.Missing if the page does not exist.
        /// Exceptions:
        /// System.ArgumentOutOfRangeException:
        /// pageNumber is negative.
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        public override DocumentPage GetPage(int pageNumber)
        {
            return ConstructPage(ContentGrid(), pageNumber);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// This function measures the heights of the page header, page footer and grid header and the first row in the grid
        /// in order to work out how manage pages might be required.
        /// </summary>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private void MeasureElements()
        {
            double allocatedSpace = 0;

            //Measure the page header
            ContentControl pageHeader = new ContentControl();
            pageHeader.Content = CreateDocumentHeader();
            allocatedSpace = MeasureHeight(pageHeader);

            //Measure the page footer
            ContentControl pageFooter = new ContentControl();
            pageFooter.Content = CreateDocumentFooter(0);
            allocatedSpace += MeasureHeight(pageFooter);

            //Include any margins
            allocatedSpace += this.PageMargin.Bottom + this.PageMargin.Top;

            //Work out how much space we need to display the grid
            _availableHeight = this.PageSize.Height - allocatedSpace;

            //Calculate the height of the first row
            _avgRowHeight = 30D;

            //Calculate how many rows we can fit on each page
            double rowsPerPage = Math.Floor(_availableHeight / _avgRowHeight);

            if (!double.IsInfinity(rowsPerPage))
                _rowsPerPage = Convert.ToInt32(rowsPerPage);

            //Count the rows in the document source
            double rowCount = _gridContentData.Count;

            //Calculate the nuber of pages that we will need
            if (rowCount > 0)
                _pageCount = Convert.ToInt32(Math.Ceiling(rowCount / rowsPerPage));
        }

        /// <summary>
        /// This method constructs the document page (visual) to print
        /// </summary>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private DocumentPage ConstructPage(Grid content, int pageNumber)
        {
            if (content == null)
                return null;

            //Build the page inc header and footer
            Grid pageGrid = new Grid();

            //Header row
            AddGridRow(pageGrid, GridLength.Auto);

            //Content row
            AddGridRow(pageGrid, new GridLength(1.0d, GridUnitType.Star));

            //Footer row
            AddGridRow(pageGrid, GridLength.Auto);

            ContentControl pageHeader = new ContentControl();
            pageHeader.Content = this.CreateDocumentHeader();
            pageGrid.Children.Add(pageHeader);

            if (content != null)
            {
                content.SetValue(Grid.RowProperty, 1);
                pageGrid.Children.Add(content);
            }

            ContentControl pageFooter = new ContentControl();
            pageFooter.Content = CreateDocumentFooter(pageNumber + 1);
            pageFooter.SetValue(Grid.RowProperty, 2);

            pageGrid.Children.Add(pageFooter);

            double width = this.PageSize.Width - (this.PageMargin.Left + this.PageMargin.Right);
            double height = this.PageSize.Height - (this.PageMargin.Top + this.PageMargin.Bottom);

            pageGrid.Measure(new Size(width, height));
            pageGrid.Arrange(new Rect(this.PageMargin.Left, this.PageMargin.Top, width, height));

            return new DocumentPage(pageGrid);
        }

        /// <summary>
        /// Measures the height of an element
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private double MeasureHeight(FrameworkElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.Measure(this.PageSize);
            return element.DesiredSize.Height;
        }

        /// <summary>
        /// Adds a row to a grid
        /// </summary>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private void AddGridRow(Grid grid, GridLength rowHeight)
        {
            if (grid == null)
                return;

            RowDefinition rowDef = new RowDefinition();

            if (rowHeight != null)
                rowDef.Height = rowHeight;

            grid.RowDefinitions.Add(rowDef);
        }

        /// <summary>
        /// Header of Grid.
        /// </summary>
        /// <returns></returns>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private Grid CreateDocumentHeader()
        {
            Grid gridHeader = new Grid();
            try
            {
                gridHeader.HorizontalAlignment = HorizontalAlignment.Stretch;
                gridHeader.VerticalAlignment = VerticalAlignment.Top;
                gridHeader.Margin = new Thickness(0D);
                gridHeader.ShowGridLines = false;

                for (int i = 0; i < 2; i++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = GridLength.Auto;
                    gridHeader.RowDefinitions.Add(rowdef); 
                }

                for (int i = 0; i < 2; i++)                             //Adding two columns in header grid.
                {
                    ColumnDefinition coldef = new ColumnDefinition();
                    coldef.Width = new GridLength(1.0D, GridUnitType.Star);
                    gridHeader.ColumnDefinitions.Add(coldef);
                }

                TextBlock txtBlockR0 = new TextBlock();
                txtBlockR0.Text = "CASH/RETIAL/TAX INVOICE";
                txtBlockR0.FontSize = 18;
                txtBlockR0.FontWeight = FontWeights.Bold;
                txtBlockR0.Height = 45D;
                txtBlockR0.Padding = new Thickness(0, 0, 0, 0);
                txtBlockR0.VerticalAlignment = VerticalAlignment.Top;
                txtBlockR0.HorizontalAlignment = HorizontalAlignment.Center;
                txtBlockR0.TextAlignment = TextAlignment.Center;
                Grid.SetRow(txtBlockR0, 0);
                Grid.SetColumn(txtBlockR0, 0);
                Grid.SetColumnSpan(txtBlockR0, 2);
                gridHeader.Children.Add(txtBlockR0);

                TextBlock txtBlockR1 = new TextBlock();
                txtBlockR1.Text = String.Format("Shop:\t{0}\nAddress:\t{1}\nPH No.:\t{2}", "Software Kaffee", "B-1/54, Sector-18, D.D Complex, Opp. Savitri Market, Noida", "120-4322460");
                txtBlockR1.FontSize = 12;
                txtBlockR1.Height = 80D;
                txtBlockR1.Padding = new Thickness(10, 0, 0, 0);
                txtBlockR1.VerticalAlignment = VerticalAlignment.Top;
                txtBlockR1.HorizontalAlignment = HorizontalAlignment.Left;
                txtBlockR1.TextAlignment = TextAlignment.Left;
                Grid.SetRow(txtBlockR1, 1);
                Grid.SetColumn(txtBlockR1, 0);
                gridHeader.Children.Add(txtBlockR1);
            }
            catch (Exception)
            {
                throw;
            }
            return gridHeader;
        }

        /// <summary>
        /// Footer of Grid.
        /// </summary>
        /// <param name="currentPageCount"></param>
        /// <returns></returns>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private Grid CreateDocumentFooter(int currentPageCount)
        {
            Grid gridFooter = new Grid();
            try
            {
                gridFooter.HorizontalAlignment = HorizontalAlignment.Center;
                gridFooter.VerticalAlignment = VerticalAlignment.Top;
                gridFooter.Margin = new Thickness(0);
                gridFooter.ShowGridLines = false;

                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength.Auto;
                gridFooter.RowDefinitions.Add(rowdef);

                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                gridFooter.ColumnDefinitions.Add(coldef);

                TextBlock footerTextBlock = new TextBlock();
                footerTextBlock.Text = String.Format("Page {0}-{1}", currentPageCount++, _pageCount);
                footerTextBlock.FontSize = 12;
                footerTextBlock.VerticalAlignment = VerticalAlignment.Bottom;
                footerTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(footerTextBlock, 0);
                Grid.SetColumn(footerTextBlock, 0);
                Grid.SetColumnSpan(footerTextBlock, 2);
                gridFooter.Children.Add(footerTextBlock);
            }
            catch (Exception)
            {
                throw;
            }
            return gridFooter;
        }

        /// <summary>
        /// Content of Grid.
        /// </summary>
        /// <returns></returns>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>08 Nov 2012</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        private Grid ContentGrid()
        {
            Grid gridContent = new Grid();
            try
            {
                int pageSize = 26;

                var data = _gridContentData.Skip(_skipRows).Take(pageSize);

                int totalRows = data.Count();

                gridContent.HorizontalAlignment = HorizontalAlignment.Stretch;
                gridContent.VerticalAlignment = VerticalAlignment.Top;
                gridContent.Margin = new Thickness(0D);
                gridContent.ShowGridLines = false;

                for (int i = 0; i < totalRows + 2; i++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = new GridLength(30D, GridUnitType.Pixel);
                    gridContent.RowDefinitions.Add(rowdef);
                }

                for (int i = 0; i < 6; i++)
                {
                    ColumnDefinition coldef = new ColumnDefinition();
                    coldef.Width = new GridLength(100D, GridUnitType.Star);
                    gridContent.ColumnDefinitions.Add(coldef);
                }

                #region << Grid Border  >>

                for (int i = 1; i < totalRows + 2; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        Border border = new Border();
                        border.BorderThickness = new Thickness(0.3D);
                        if (i == 1)
                        {
                            border.Background = Brushes.Cornsilk;
                        }
                        border.BorderBrush = Brushes.Black;
                        border.SetValue(Grid.ZIndexProperty, -1);
                        Grid.SetRow(border, i);
                        Grid.SetColumn(border, j);
                        gridContent.Children.Add(border);
                    }
                }

                #endregion

                #region <<  Report Title  >>

                TextBlock txtBlockTitle = new TextBlock();
                txtBlockTitle.Text = String.Format("Invoice Information for {0}", this._tinNumber);
                txtBlockTitle.FontSize = 15;
                txtBlockTitle.FontWeight = FontWeights.Bold;
                txtBlockTitle.VerticalAlignment = VerticalAlignment.Center;
                txtBlockTitle.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(txtBlockTitle, 0);
                Grid.SetColumn(txtBlockTitle, 0);
                Grid.SetColumnSpan(txtBlockTitle, 6);
                gridContent.Children.Add(txtBlockTitle);

                #endregion

                #region <<  Grid Header  >>

                TextBlock txtBlockHeader = null;

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "SNo #";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(10, 0, 0, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Left;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 0);
                gridContent.Children.Add(txtBlockHeader);

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "Order Number";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(10, 0, 0, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Left;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 1);
                gridContent.Children.Add(txtBlockHeader);

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "Order Date";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(0, 0, 10, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Right;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 2);
                gridContent.Children.Add(txtBlockHeader);

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "Customer Number";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(0, 0, 0, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 3);
                gridContent.Children.Add(txtBlockHeader);

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "Customer Address";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(0, 0, 0, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 4);
                gridContent.Children.Add(txtBlockHeader);

                txtBlockHeader = new TextBlock();
                txtBlockHeader.Text = "Warranty";
                txtBlockHeader.FontSize = 13;
                txtBlockHeader.FontWeight = FontWeights.Bold;
                txtBlockHeader.Padding = new Thickness(10, 0, 0, 0);
                txtBlockHeader.VerticalAlignment = VerticalAlignment.Center;
                txtBlockHeader.HorizontalAlignment = HorizontalAlignment.Left;
                Grid.SetRow(txtBlockHeader, 1);
                Grid.SetColumn(txtBlockHeader, 5);
                gridContent.Children.Add(txtBlockHeader);

                #endregion

                #region <<  Grid Content  >>

                int rowCount = 2;

                foreach (var item in data)
                {
                    TextBlock txtBlock = null;

                    if (item == null)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            txtBlock = new TextBlock();
                            txtBlock.Text = String.Empty;
                            Grid.SetRow(txtBlock, rowCount);
                            Grid.SetColumn(txtBlock, 0);
                            gridContent.Children.Add(txtBlock);
                        }
                        rowCount++;
                        continue;
                    }

                    #region <<  Column Sno.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = item.Sno;
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(10, 0, 0, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 0);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    #region <<  Column Order Number.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = Convert.ToString(item.OrderNumber);
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(10, 0, 0, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 1);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    #region <<  Column Orde rDate.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = item.OrderDate==DateTime.MinValue?String.Empty:item.OrderDate.ToShortDateString(); 
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(0, 0, 10, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 2);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    #region <<  Column Customer Name.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = item.CustomerName;
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(0, 0, 0, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Center;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 3);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    #region <<  Column Address.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = item.Address;
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(0, 0, 0, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Center;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 4);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    #region <<  Column Warranty.  >>

                    txtBlock = new TextBlock();
                    txtBlock.Text = item.Warranty==true?"Y":"N";
                    txtBlock.FontSize = 12;
                    txtBlock.Padding = new Thickness(10, 0, 0, 0);
                    txtBlock.VerticalAlignment = VerticalAlignment.Top;
                    txtBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    Grid.SetRow(txtBlock, rowCount);
                    Grid.SetColumn(txtBlock, 5);
                    gridContent.Children.Add(txtBlock);

                    #endregion

                    rowCount++;
                }
                _skipRows += pageSize;

                #endregion
            }
            catch (Exception)
            {
                throw;
            }
            return gridContent;
        }

        #endregion
    }
}

***************************************************************


No comments: