Wednesday, May 8, 2013
Hide and Show RDLC Column And Adjust Width
#region Adjust Report Column Width When Some Columns Hide
//adjust hide column width into report
private void AdjustRDLCReport(string rptFilePath, bool isOnlyLabelChange, int rowNumberOfTable)
{
try
{
//Delete Duplicate Report File File
if (System.IO.File.Exists(virtualreportFilePath))
System.IO.File.Delete(virtualreportFilePath);
//get rdlc report xml
XmlDocument objXml = new XmlDocument();
objXml.Load(rptFilePath);
//set name space to change xml node
XmlNamespaceManager objXmlNamespaceManager = new XmlNamespaceManager(objXml.NameTable);
string uri = "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition";
objXmlNamespaceManager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
objXmlNamespaceManager.AddNamespace("rdef", uri);
//if no header visibile,than no change into report xml
if (reportColumnList == null || reportColumnList.Count == 0)
{
//save temporary report as virtual
objXml.Save(virtualreportFilePath);
return;
}
//if only header change
if (isOnlyLabelChange)//only hader change no need column hide or show
{
//Change Header
RDLCReportLabelChange(objXml, objXmlNamespaceManager);
}
else
{
//Change Header
RDLCReportLabelChange(objXml, objXmlNamespaceManager);
//Set visible column
XmlNodeList columnList = objXml.SelectNodes("//rdef:TablixBody/rdef:TablixRows/rdef:TablixRow[" + rowNumberOfTable + "]/rdef:TablixCells/rdef:TablixCell/rdef:CellContents/rdef:Textbox/rdef:Paragraphs/rdef:Paragraph/rdef:TextRuns/rdef:TextRun/rdef:Value", objXmlNamespaceManager);
//Hide Or Show Report Column
RDLCReportColumnHideShow(columnList, objXml, objXmlNamespaceManager, uri);
//Visible Report Column Width Adjust
RDLCReportColumnWidthAdjust(columnList, objXml, objXmlNamespaceManager);
}
//save temporary report as virtual
objXml.Save(virtualreportFilePath);
}
catch (Exception)
{
throw;
}
}//end of method
//Rdlc report label change according to label.xml
private void RDLCReportLabelChange(XmlDocument objXml, XmlNamespaceManager objXmlNamespaceManager)
{
XmlNodeList reportHeaderList = objXml.SelectNodes("//rdef:TextRun/rdef:Value", objXmlNamespaceManager);
if (reportHeaderList == null) return;
foreach (XmlNode node in reportHeaderList)
{
var reportColumn = reportColumnList.SingleOrDefault(x => x.Key == node.InnerText);
if (reportColumn.Key == null) continue;
node.InnerText = reportColumn.Value;
}
}
//RDLC report column hide or show
private void RDLCReportColumnHideShow(XmlNodeList columnList, XmlDocument objXml, XmlNamespaceManager objXmlNamespaceManager, string uri)
{
XmlNodeList columnVisibleList = objXml.SelectNodes("//rdef:TablixColumnHierarchy/rdef:TablixMembers/rdef:TablixMember", objXmlNamespaceManager);
if (columnList != null)
{
if (columnVisibleList != null)
{
for (int i = 0; i < columnList.Count; i++)
{
var reportColumn = reportColumnList.SingleOrDefault(x => String.Format("=Fields!{0}.Value", x.Key) == columnList[i].InnerText);
if (reportColumn.Key == null)
{
for (int j = 0; j < columnVisibleList.Count; j++)
{
if (i == j)
{
XmlElement nVis = objXml.CreateElement("Visibility", uri);
XmlElement nHid = objXml.CreateElement("Hidden", uri);
nHid.InnerText = "true";
nVis.AppendChild(nHid);
columnVisibleList[j].AppendChild(nVis);
break;
}
}//end of column visibility
}
}//end of all column
}
}
}//end of method
//RDLC Report Column Width Adjustment according to visible column
private void RDLCReportColumnWidthAdjust(XmlNodeList columnList, XmlDocument objXml, XmlNamespaceManager objXmlNamespaceManager)
{
XmlNodeList columnWidthList = objXml.SelectNodes("//rdef:TablixBody/rdef:TablixColumns/rdef:TablixColumn/rdef:Width", objXmlNamespaceManager);
double hideColumnWidth = 0;
int NoOfVisibleColumn = 0;
if (columnList != null)
{
if (columnWidthList != null)
{
for (int i = 0; i < columnList.Count; i++)
{
var reportColumn = reportColumnList.SingleOrDefault(x => String.Format("=Fields!{0}.Value", x.Key) == columnList[i].InnerText);
if (reportColumn.Key == null)
{
for (int j = 0; j < columnWidthList.Count; j++)
{
if (i == j)
{
string colWidth = columnWidthList[j].InnerText;
hideColumnWidth += Convert.ToDouble(colWidth.Remove(colWidth.Length - 2));
break;
}
}//end of column visibility
}
else
{
NoOfVisibleColumn++;//number fo visible column
}
}
}//end of if
}//end of if
//Distribute Hode Column width into visible column width
if (columnList != null)
{
if (columnWidthList != null)
{
for (int i = 0; i < columnList.Count; i++)
{
var reportColumn = reportColumnList.SingleOrDefault(x => String.Format("=Fields!{0}.Value", x.Key) == columnList[i].InnerText);
if (reportColumn.Key != null)
{
for (int j = 0; j < columnWidthList.Count; j++)
{
if (i == j)
{
string colWidth = columnWidthList[j].InnerText;
double actualVisibleColumnWidth = Convert.ToDouble(colWidth.Remove(colWidth.Length - 2)) + (hideColumnWidth / NoOfVisibleColumn);
columnWidthList[j].InnerText = String.Format("{0}in", actualVisibleColumnWidth);
break;
}
}//end of column visibility
}
}
}//end of if
}//end of if
}//end of method
#endregion
calling
//This Dictionary Contains Report Header,That is Provided from Personalized Column Grid.
private Dictionary<string, string> reportColumnList = new Dictionary<string, string>();
private string virtualreportFilePath = String.Empty;
AdjustRDLCReport(reportFilePath, false, 2);// false is label change and hide column,if true only label change,here 2 is search for row number of tablix which is contains actual attribute of object
localReport.ReportPath = virtualreportFilePath;
Labels:
Silverlight
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment