none
c# Report Generator RRS feed

All replies

  • Hello,

    See ReportViewer, the designer is an Visual Studio extension.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Dolen Zhang Wednesday, June 20, 2018 9:09 AM
    Monday, June 11, 2018 12:39 PM
    Moderator
  • Hello,

     

    Thanks for posting here.

     

    From your simple words, we cannot know what help you need exactly? Could you clarify it so that others can help you correspondingly?

    Regards,

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, June 12, 2018 9:17 AM
  • Hi,

    If you want to use report viewer, then I made a simple demo and please refer to it as below:

    Step 1: Create a class:

    class Customers
    {
            public string CustomerId { get; set; }
            public string ContactName { get; set; }
            public string City { get; set; }
            public string Country { get; set; }
    }

    Step 2: Drag the report viewer control from toolbox to Form.

    Step 3: Create CustomReport.rdlc: Right click>Add>Add new Item>create CustomReport.rdlc.

    Then insert a Customers table for it just like:

    Step 4: In Form, right click report viewer control >Choose Report>CustomReport.rdlc

    Step 5: Code in Main Form:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                List<Customers> list = new List<Customers>();
                list = GetData();
                reportViewer1.LocalReport.DataSources.Clear(); //clear report
                reportViewer1.LocalReport.ReportEmbeddedResource = "WinFormTest3.CustomReport.rdlc"; // bind reportviewer with .rdlc
                ReportDataSource dataset = new ReportDataSource("DataSet1", list); // set the datasource
                reportViewer1.LocalReport.DataSources.Add(dataset);
                dataset.Value = list;
    
                reportViewer1.LocalReport.Refresh();
                reportViewer1.RefreshReport(); // refresh report
            }
    
            private List<Customers> GetData()
                {
                    string constr = @"Server=[user]\SQLEXPRESS;Database=MyDB;Trusted_Connection=Yes";
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM customers"))
                        {
                            using (SqlDataAdapter sda = new SqlDataAdapter())
                            {
                              cmd.Connection = con;
                              sda.SelectCommand = cmd;
                            DataTable dt = new DataTable();
                            List<Customers> list = new List<Customers>();
                            Customers customer = new Customers();
                            sda.Fill(dt);
                            foreach(DataRow row in dt.Rows)
                            {
                                customer.CustomerId = row["CustomerId"].ToString();
                                customer.ContactName = row["ContactName"].ToString();
                                customer.City = row["City"].ToString();
                                customer.Country = row["Country"].ToString();
                                list.Add(customer);
                            }
                            return list;
                            }
                        }
                    }
                }       
        }

    Click the print button, if you see the print page number is greater than data page number. You could change it to data page number.

    Just like:

    Also please refer to the following document:

    How to: Print in Windows Forms Using Print Preview.

    Best Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Wednesday, July 4, 2018 6:12 AM
    Wednesday, July 4, 2018 6:11 AM