Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / Visual Basic

A New .NET Reporting Way

Rate me:
Please Sign up or sign in to vote.
4.67/5 (50 votes)
29 Sep 2007CPOL3 min read 405.6K   17.5K   248  
Looking for a free and simple way to design and add reports to your .NET application? Take a look at MyNeoReport library.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using NeoDataType.MyNeoReport;
// This is to avoid ambiguity between 
// System.Windows.Form.Label/Image and NeoDataType.MyNeoReport.Label/Image
using Rpt = NeoDataType.MyNeoReport;

namespace SimpleDesigner
{
    public partial class MainForm : Form
    {

        Report _report = new Report();
        Section _workingSection;

        const string FILE_FILTER = "MyNeoReport Documents|*.mr5|" +
                                   "All files|*.*";

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pageControl1.SetPage(_report.Page);
        }




        private void showPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Select the destination printer
            _report.ShowPrinterDialog();

            // Show the preview
            _report.ShowPreview();
        }

        private void addGroupSectionToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // TODO: Set the grouping fields
            string fieldList = "";
            GroupSection groupSection = _report.Page.AddGroupSection();
            groupSection.GroupFieldList = fieldList;

        }



        private void addLabelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            if (_workingSection != null)
            {
                Rpt.Label label = _workingSection.AddLabel();

                // Example of how you can control the item via code:
                // - Set the text
                label.Text = "Label added at " + DateTime.Now.ToString();

                // - Set horizontal autosize
                label.HorizontalSizeMode = TextHorizontalSizeMode.TextWidth;
            }

        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Clear the page and sections
            _report.Page.Clear();
        }

        private void bringToFrontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _report.Page.SelectedItem.BringToFront();
        }

        private void sendToBackSelectedItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _report.Page.SelectedItem.SendToBack();
        }

        private void addImageToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (_workingSection != null)
            {
                Rpt.Image image = _workingSection.AddImage();

                if (_report.FileName.Length > 0)
                {

                    // You can embed the image into the report setting
                    // image.UseImageFile = false (default), otherwise 
                    // the report will search for the physical file.
                    image.UseImageFile = true;

                    // One way to load images...
                    // You can also set it just passing the
                    // image data to image.RawImage
                    // {@ReportPath} is a special embedded function
                    // as described in the NeoDataType forum:
                    // http://www.neodatatype.net/forum/topic.asp?TOPIC_ID=149
                    image.ImageFile = "{@ReportPath}\\neodatatype_logo.png";
                    
                }
            }
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }

        private void pageControl1_ItemSelected(object sender, ItemSelectedEventArgs e)
        {
            propertyGrid1.SelectedObject = e.Item;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.FileName = "";
            openFile.Filter = FILE_FILTER;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                _report.LoadFrom(openFile.FileName);

                // Force the refresh of the Page control
                pageControl1.SetPage(_report.Page);
            }

            openFile.Dispose();

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_report.FileName.Length > 0)
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.FileName = _report.FileName;
                saveFile.Filter = FILE_FILTER;

                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    _report.SaveTo(saveFile.FileName);
                }

                saveFile.Dispose();

            }

        }

        private void deleteBringToFrontSelectedItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Get the selected Item
            ItemBase selectedItem = _report.Page.SelectedItem;

            // Remove it from its section
            selectedItem.Section.RemoveItem(selectedItem);
        }

        private void addShapeToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (_workingSection != null)
            {
                Shape shape = _workingSection.AddShape();

                // Example of how you can control the item via code:
                // - Set the shape to be drawn
                shape.ShapeType = ShapeType.RoundRectangle;

                // - Use antialias: this can be useful for rotated items
                shape.AntiAlias = true;
            }
        }

        private void addDataLabelToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (_workingSection != null)
            {
                // This show that you can add an item also by a new istance:
                DataLabel dataLabel = new DataLabel();

                // Example of how you can control the item via code:
                // - Set the datafield on which the datalabel is binded
                dataLabel.DataField = "ProductName";

                _workingSection.AddItem(dataLabel);

            }
        }

        private void addDataImageToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (_workingSection != null)
            {
                // This show that you can add an item also by a new istance:
                DataImage dataImage = new DataImage();

                // Example of how you can control the item via code:
                // - Set the datafield on which the dataImage is binded
                // This field can be a simple blob field containing 
                // a raw image (in for of byte array) or an OLE picture
                dataImage.DataField = "Picture";

                _workingSection.AddItem(dataImage);

            }
        }

        private void setDetailsConnectionStringToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // Default datasource is OleDbDataSource that let you connect to OleDb providers.
            // You can choose to pass data to a datasource changing it to a TableDataSource
            // with section.ChangeDatasourceType(DataSourceType.UseDataTable)
            OleDbDataSource dataSource = (OleDbDataSource)_report.Page.Details.DataSource;
            dataSource.ShowConnectionStringWizard();

            propertyGrid1.SelectedObject = _report.Page.Details.DataSource;

            MessageBox.Show("Now you need to edit the SQLText property of the Details DataSource in the property grid",
                            "Details Datasource", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void selectTheWorkingSectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the working section
            BrowseSectionForm sectionListForm = new BrowseSectionForm(_report.Page);
            if (sectionListForm.ShowDialog() == DialogResult.OK)
            {
                _workingSection = sectionListForm.SelectedSection;
                selectTheWorkingSectionToolStripMenuItem.Text = "Select the working section [" + _workingSection.Name + "]";
                propertyGrid1.SelectedObject = _workingSection;
            }
            sectionListForm.Dispose();

        }

        private void moveSelectedItemToTheSectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the destination section
            BrowseSectionForm sectionListForm = new BrowseSectionForm(_report.Page);
            if (sectionListForm.ShowDialog() == DialogResult.OK)
            {
                _report.Page.SelectedItem.MoveToSection(sectionListForm.SelectedSection);
            }
            sectionListForm.Dispose();
        }


    }
}

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
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions