Thursday, May 2, 2013

How do I adjust overall width of rdlc report when some columns are hidden?

        Problem:Lets say that I have 10 columns to view report and I want to hide 3 of these columns at runtime based on the value of parameter which the user would select. This can be easily done by setting the column visibility of each of these 3 columns based on the value of the aforesaid parameter. It's perfectly fine up till here.
The problem is when the report shows up (with 3 columns hidden) the remaining 7 columns take up the place of the hidden columns and as a result the overall width of the table reduces accordingly. I do not want this to happen. i.e. I want the table width to remain constant.That is to say the remaining columns width should somehow be able to expand so that the original overall width of the table remains same.Is this possible to achieve?
Solution:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                String sFileName = String.Empty;
                XmlDocument objXml = new XmlDocument();
                sFileName = @"c:\users\sohel\documents\visual studio 2012\Projects\EditRdlcReport\EditRdlcReport.Web\Reports\Report1.rdlc";
                objXml.Load(sFileName);

                XmlNamespaceManager objXmlNamespaceManager = new XmlNamespaceManager(objXml.NameTable);
                objXmlNamespaceManager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
                objXmlNamespaceManager.AddNamespace("reportDefinition", "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition");
                //strUniquelyIdentifiedParentTagName is the control name like TextBox, Image etc.
                XmlNodeList nodeList = objXml.SelectNodes("//reportDefinition:" + "TablixColumn", objXmlNamespaceManager);
                // Nodelist will contain all Textboxes of the report.
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        if (node["Width"].Name == "Width")
                        {
                            node["Width"].InnerText = "2.58167in";
                        }
                    }
                }

                // establish some file names              
                string virtualRdlc = "~/Reports/" + "Report2" + ".rdlc";
                // save off the resultng RDLC file
                string physicalRdlc = Server.MapPath(virtualRdlc);
                //Delete Duplicate File
                if (System.IO.Directory.Exists(@"c:\users\sohel\documents\visual studio 2012\Projects\EditRdlcReport\EditRdlcReport.Web\Reports\Report2.rdlc"))
                    System.IO.Directory.Delete(physicalRdlc);

                //save virtually new
                objXml.Save(physicalRdlc);

                LocalReport localReport = new LocalReport();
                localReport.ReportPath = Server.MapPath("~/Reports/" + "Report2" + ".rdlc");
                RenderRDLCReport(new Student().GetStudent(), "Report2", localReport);
            }
        }


#region Generae Report

        private void RenderRDLCReport<T>(List<T> objList, string reportFileName, LocalReport localReport)
        {
            string DataSetName = String.Empty;
            DataSetName = "DataSet1";

            ReportDataSource reportDataSource = new ReportDataSource(DataSetName, objList);
            localReport.DataSources.Add(reportDataSource);

            var reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            //The DeviceInfo settings should be changed based on the reportType
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
            string deviceInfo =
            "<DeviceInfo>" +
            " <OutputFormat>PDF</OutputFormat>" +
            " <PageWidth>11in</PageWidth>" +
            " <PageHeight>8.5in</PageHeight>" +
            " <MarginTop>0.5in</MarginTop>" +
            " <MarginLeft>1in</MarginLeft>" +
            " <MarginRight>1in</MarginRight>" +
            " <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            var renderedBytes = localReport.Render(
                                reportType,
                                deviceInfo,
                                out mimeType,
                                out encoding,
                                out fileNameExtension,
                                out streams,
                                out warnings);

            //Clear the response stream and write the bytes to the outputstream
            //Set content-disposition to "attachment" so that user is prompted to take an action
            //on the file (open or save)
            Response.Clear();
            Response.ContentType = mimeType;

            Response.BinaryWrite(renderedBytes);
            Response.End();
        }

        #endregion

2 comments:

  1. Hi, thanks for this. I realized that you were actually changing the width of the TablixColumn to

    node["Width"].InnerText = "2.58167in";

    Is there any settings in the TablixColumn that we can set as "Auto" or "*" like in XAML to avoid hard coding the width?

    ReplyDelete