0

I have Created a report dynamically from c# and done it successfully, Now the problem is it is aligned in Right and i wish to bring it at the center. How to do that ?

My code is here :

 private void ShowReport()
 {
      ReportDataSource rds = new ReportDataSource("MyData", m_dataSet.Tables[0]);
      this.reportViewer1.LocalReport.DataSources.Add(rds);

      this.reportViewer1.RefreshReport();

      this.reportViewer1.LocalReport.LoadReportDefinition(m_rdl);
      reportViewer1.LocalReport.GetDefaultPageSettings().Margins.Right = 125; //Has no effect 
      this.reportViewer1.RefreshReport();   
 }

I also tried :

 public static Rdl.Report CreateReport(string groupBy)
        {
            Rdl.Report report = new Rdl.Report();
            try
            {
                report.Items = new object[] 
                {
                    CreateDataSources(), 
                    CreateHeader(groupBy),  
                    CreateBody(groupBy), 
                    CreateDataSets(), 
                    "9.5in", 
                    "1.5in",
                };
                report.ItemsElementName = new Rdl.ItemsChoiceType37[]
                { 
                    Rdl.ItemsChoiceType37.DataSources, 
                    Rdl.ItemsChoiceType37.PageHeader,
                    Rdl.ItemsChoiceType37.Body,
                    Rdl.ItemsChoiceType37.DataSets,
                    Rdl.ItemsChoiceType37.Width,
                    Rdl.ItemsChoiceType37.LeftMargin,

                };
            }
            catch (Exception ex) { }
            return report;
        }

please note that i do not have to set the alignment of reportviewer, I have to set the the alignment of report on reportviewer.

test test
  • 141
  • 4
  • 15

2 Answers2

1

Without having to calculate your documents and work area sizes, I found out that the following properties of the ReportViewer will do the trick.

Place this code in the form's Initialization method.

ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
ReportViewer1.ZoomMode = ZoomMode.PageWidth;

This will align the document in the center and fit it gracefully with the form's size.

fcdt
  • 2,371
  • 5
  • 14
  • 26
Sierra
  • 71
  • 3
0

I'm not sure if it's possible using any kind of ReportViewer control property. Mostly I just eyeball it, but if precision is a priority for you: there is a bit of code I have used before that did the trick.

A workaround if you will;

private void ReportViewer1_Resize(object sender, EventArgs e)
    {
        // Center report
        ReportPageSettings rps = ReportViewer1.LocalReport.GetDefaultPageSettings();
        if (ReportViewer1.ParentForm.Width > rps.PaperSize.Width)
        {
            int hPad = (ReportViewer1.ParentForm.Width - rps.PaperSize.Width) / 2;
            ReportViewer1.Padding = new Padding(hPad, 1, hPad, 1);
        }

Edit: Btw, you can essentially use the code with most methods. Let me know if it works for you.

Edit#2: I was also unsure about whether or not you were talking about the table, or the ReportViewer itself that you wanted to center. Hopefully it works for you.

iDillon
  • 122
  • 11
  • I already tried this.it sets the alignment of reportviewer. but i want the report at center of reportviewer (i mean contents of report) – test test May 17 '16 at 13:33
  • Ah ok, yeah, there's no neat way of accomplishing that dynamically. I suggesting making the reportViewer the same size as your table, or control that you use. – iDillon May 17 '16 at 13:38