Code Snippet
/// <summary>
/// Replaces the chart type in the report with the chart type passed to this method.
/// </summary>
/// <param name="documentToUpdate">The report.rdlc (xml document) you want to change the chart type for.</param>
/// <param name="desiredChartType">The desired chart type, don't worry about blank strings, it is set to a default in the function.</param>
/// <param name="reportNamespace">The namespace of the report. You can find this by opening your .rdlc file in an xml browser.</param>
/// <param name="listName">The name of the list object that you included in your report. If your components are not using a list, pass null.</param>
/// <param name="chartName">The name of the chart you want to adjust the type for.</param>
/// <param name="viewerToUpdate">The ReportViewer object you are going to add this report to.</param>
public static void UpdateReportChartType(XmlDocument documentToUpdate, string desiredChartType, string reportNamespace, string listName, string chartName,
ReportViewer viewerToUpdate)
{
XmlElement root = documentToUpdate.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager(documentToUpdate.NameTable);
nsManager.AddNamespace("ns", reportNamespace);
XmlNode workingNode;
//If component does not use a list, leave out the list item by making the list string blank.
string listToUse;
if ((listName == null) || (listName == ""))
listToUse = "";
else
listToUse = "ns:ReportItems/ns:List[@Name='" + listName + "']/";
//Set the chart type.
workingNode = root.SelectSingleNode("ns:Body/" + listToUse + "ns:ReportItems/ns:Chart[@Name='" + chartName + "']/ns:Type", nsManager);
if (desiredChartType == "")
workingNode.InnerText = DefaultChartType;
else
workingNode.InnerText = desiredChartType;
//Save original settings to restore after resetting.
ProcessingMode originalProcessingMode = viewerToUpdate.ProcessingMode;
ReportDataSource originalDataSource = viewerToUpdate.LocalReport.DataSources[0];
//Reset the basic report information.
viewerToUpdate.Reset();
viewerToUpdate.ProcessingMode = originalProcessingMode;
viewerToUpdate.LocalReport.DataSources.Add(originalDataSource);
viewerToUpdate.LocalReport.ReportPath = string.Empty;
viewerToUpdate.LocalReport.LoadReportDefinition(new StringReader(documentToUpdate.OuterXml));
viewerToUpdate.LocalReport.Refresh();
}