Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Customizing and Localizing Microsoft ReportViewer .NET - An Exact Solution

Rate me:
Please Sign up or sign in to vote.
4.67/5 (16 votes)
11 Feb 2007CPOL2 min read 129.6K   3.2K   55  
An article about How to Customizing and Localizing Microsoft ReportViewer Toolbar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace LocReport
{
    public partial class ReportViewerForm : Form
    {
        private CustomReportViewerMessages customMessages = null;

        public ReportViewerForm()
        {
            InitializeComponent();
            customizeMessages();
            customizeReportViewer();

        }

        private void customizeMessages()
        {
            this.customMessages = new CustomReportViewerMessages(this.reportViewer.Messages);
            this.customMessages.FindButtonText = "Bul";
            this.customMessages.FindButtonToolTip = "Girilen metni bul";
            this.customMessages.FindNextButtonText = "Sonraki";
            this.customMessages.FindNextButtonToolTip = "Ayn� metni tekrar bul";

            this.customMessages.SearchTextBoxToolTip = @"Aranacak metni yaz�n�z";
            this.customMessages.TextNotFound = @"Aranan metin bulunamad�";
            this.customMessages.NoMoreMatches = @"Ba�ka e�le�me bulunamad�";
            this.customMessages.BackButtonToolTip = @"Geri";
            this.customMessages.BackMenuItemText = @"Geri";
            this.customMessages.DocumentMapButtonToolTip = @"D�k�man gezgini";
            this.customMessages.DocumentMapMenuItemText = @"D�k�man gezgini";
            this.customMessages.ExportButtonToolTip = @"Aktar";
            this.customMessages.ExportMenuItemText = @"Aktar";
            this.customMessages.FalseValueText = @"Yanl��";
            this.customMessages.FirstPageButtonToolTip = @"�lk Sayfa";
            this.customMessages.LastPageButtonToolTip = @"Son Sayfa";
            this.customMessages.NextPageButtonToolTip = @"Sonraki Sayfa";
            this.customMessages.PageOf = @"/";
            this.customMessages.PageSetupButtonToolTip = @"Sayfa Ayarlar�";
            this.customMessages.PageSetupMenuItemText = @"Sayfa Ayarlar�";
            this.customMessages.ParameterAreaButtonToolTip = @"Parametreler";
            this.customMessages.PasswordPrompt = @"�ifre";
            this.customMessages.PreviousPageButtonToolTip = @"�nceki Sayfa";
            this.customMessages.PrintButtonToolTip = @"Yazd�r";
            this.customMessages.PrintLayoutButtonToolTip = @"Yazd�rma G�r�n�m�";
            this.customMessages.PrintLayoutMenuItemText = @"Yazd�rma G�r�n�m�";
            this.customMessages.PrintMenuItemText = @"Yazd�r";
            this.customMessages.ProgressText = @"Rapor Haz�rlan�yor";
            this.customMessages.RefreshButtonToolTip = @"Yenile";
            this.customMessages.RefreshMenuItemText = @"Yenile";
            this.customMessages.SelectAll = @"T�m�n� Se�";
            this.customMessages.SelectAValue = @"Se�";
            this.customMessages.StopButtonToolTip = @"Dur";
            this.customMessages.StopMenuItemText = @"Dur";
            this.customMessages.TextNotFound = @"Metin Bulunamad�";
            this.customMessages.TotalPagesToolTip = @"Toplam Sayfa";
            this.customMessages.TrueValueText = @"Do�ru";
            this.customMessages.UserNamePrompt = @"Kullan�c� Ad�";
            this.customMessages.ViewReportButtonText = @"Rapor G�ster";
            this.customMessages.ViewReportButtonToolTip = @"Rapor G�ster";
            this.customMessages.ZoomControlToolTip = @"Yak�nla�t�r";
            this.customMessages.ZoomMenuItemText = @"Yak�nla�t�r";
            this.customMessages.ZoomToPageWidth = @"Sayfa Geni�li�i";
            this.customMessages.ZoomToWholePage = @"T�m Sayfa";
        }

        private void customizeReportViewer()
        {
            ToolStrip toolStrip = this.ToolStrip;

            if (toolStrip != null)
            {
                toolStrip.ImageList = this.reportViewerImageList;

                toolStrip.Items["find"].ImageKey = "btnFind.Image";
                toolStrip.Items["findNext"].ImageKey = "btnFindNext.Image";

                int zoomPos = toolStrip.Items.IndexOfKey("zoom");
                if (zoomPos >= 0)
                {
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultSeparator("additionalSeparator1"));
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultLabel("lblSeparator", String.Empty, 100));
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultSeparator("additionalSeparator2"));
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultButton("btnClose", "Close", "btnClose.Image", new EventHandler(ButtonClose)));
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultButton("btnHelp", "Help", "btnHelp.Image", null));
                    toolStrip.Items.Insert(zoomPos + 1, this.addDefaultSeparator("additionalSeparator3"));
                }

                ToolStripDropDownButton exportButton = toolStrip.Items["export"] as ToolStripDropDownButton;
                if (exportButton != null)
                    exportButton.DropDownOpened += new EventHandler(this.OnExportOpened);
            }
        }

        private ToolStripButton addDefaultButton(string name, string text, string imageResource, EventHandler eventHandler)
        {
            ToolStripButton result = new ToolStripButton();

            result.Name = name;
            result.Size = new System.Drawing.Size(23, 22);
            result.Text = text;

            if (imageResource == String.Empty)
                result.DisplayStyle = ToolStripItemDisplayStyle.Text;
            else
            {
                result.DisplayStyle = ToolStripItemDisplayStyle.Image;
                result.ImageKey = imageResource;
            }

            if (eventHandler != null)
                result.Click += eventHandler;

            return result;
        }

        private ToolStripLabel addDefaultLabel(string name, string text)
        {
            return addDefaultLabel(name, text, 0);
        }
        private ToolStripLabel addDefaultLabel(string name, string text, int width)
        {
            ToolStripLabel result = new ToolStripLabel();

            result.Name = name;
            result.Text = text;

            result.AutoSize = (width <= 0);

            if (!result.AutoSize)
                result.Size = new System.Drawing.Size(width, 22);

            return result;
        }

        private ToolStripSeparator addDefaultSeparator(string name)
        {
            ToolStripSeparator result = new ToolStripSeparator();

            result.Name = name;
            result.Size = new System.Drawing.Size(6, 25);

            return result;
        }

        private TClass FindToolStrip<TClass>(Control control)
            where TClass : System.Windows.Forms.Control
        {
            if (control == null)
                return default(TClass);
            else if (control is TClass)
                return (TClass)control;
            else
            {
                TClass result = default(TClass);
                foreach (Control embedded in control.Controls)
                {
                    if (result == null)
                        result = FindToolStrip<TClass>(embedded);
                }

                return result;
            }
        }


        public void InitializeReport(string formName, string reportDisplayName, string reportPath, ReportDataSource reportDataSource)
        {

            this.Text = formName;

            this.reportViewer.Reset();
            this.reportViewer.LocalReport.DisplayName = reportDisplayName;

            this.reportViewer.DocumentMapCollapsed = true;


            this.reportViewer.LocalReport.ReportPath = reportPath;
            this.reportViewer.Messages = this.customMessages;
            this.reportViewer.LocalReport.DataSources.Add(reportDataSource);
            this.reportViewer.RefreshReport();
        }



        private void ReportViewerForm_Load(object sender, EventArgs e)
        {
        }
        private void ButtonClose(object sender, EventArgs e)
        {
            this.Close();
        }
        private void OnExportOpened(object sender, EventArgs e)
        {
            if (sender is ToolStripDropDownButton)
            {
                ToolStripDropDownButton button = (ToolStripDropDownButton)sender;

                foreach (ToolStripItem item in button.DropDownItems)
                {
                    RenderingExtension extension = (RenderingExtension)item.Tag;
                    if (extension != null)
                    {
                        switch (extension.Name)
                        {
                            case "Excel":
                                item.Image = this.reportViewerImageList.Images["btnExcel.Image"];
                                break;
                            case "PDF":
                                item.Image = this.reportViewerImageList.Images["btnPdf.Image"];
                                break;
                        }
                    }
                }
            }
        }
        private ToolStrip ToolStrip
        {
            get { return this.FindToolStrip<ToolStrip>(this.reportViewer ); }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) MikroKom Yazilim A.S.
Turkey Turkey
c# , vb6 , mssql , oracle , mysql , asp , asp.net developer.

Comments and Discussions