This is a migrated thread and some comments may be shown as answers.
ReportViewer Palette
3 Answers 9 Views
This is a migrated thread and some comments may be shown as answers.
Christine
Top achievements
Rank 1
Christine asked on 08 Dec 2016, 04:31 PM

I'm not sure exactly which forum/discussion to ask this in. If I'm in the wrong place, I'm sorry.

I've built a WinForms app that I want to display some reports using the ReportViewer. What I want to do is set the palette for these reports using a custom palette class I've added to the WinForms project.

I have the reports in their own project so that if users decide they want a web app I can easily reuse the reports (hopefully).

I don't know how to set the palette on the report and i can't find anything online about how to do this. I know I can go into each report and set the colors individually but that is not efficient or scalable.

My code in the Winforms app to set the report in the ReportViewer is like this...

var reportSource = new ReportsLibrary.MyReport();
reportSource.ReportParameters["MyParam"].Value = value;
myReportViewer.ReportSource = reportSource;
myReportViewer.RefreshReport();

 

I figured out how to use the custom palette with charts within the winforms app like this...

myChart.Area.View.Palette = new CustomPalette();

 

I want to be able to do something similar in the Winforms app. I don't want to have to copy my CustomPalette class over to my ReportsLibrary project.

Is this possible?

I want to be able to do something like...

reportSource.Palette = new CustomPalette();

 

VS2013

Version R3 2016 (2016.3.914.1)

3 Answers, 1 is accepted

Sort by
0
Accepted
Stef
Telerik team
answered on 09 Dec 2016, 03:20 PM
Hello Christine,

Your question is forwarded to Telerik Reporting.

You can modify the report instance at run-time e.g.:
//create an instance of the VS Report
var reporInstance = new ReportsLibrary.MyReport();
 
//get the nested Graph item
var graphItem = reportInstance.Items.Find("Graph1",true)[0] as Telerik.Reporting.Graph;
//create color palette
Telerik.Reporting.Drawing.ColorPalette col = new Telerik.Reporting.Drawing.ColorPalette();
 col.Colors.Add(System.Drawing.Color.DodgerBlue);
 col.Colors.Add(System.Drawing.Color.Red);
 col.Colors.Add(System.Drawing.Color.Purple);
 
//use the color palette
 graphItem.ColorPalette = col;
 
//create a Report Source object
var reportSource = new InstanceReportSource { ReportDocument = reportInstance};
//will be mapped to the report's ReportParameters collection
reportSource.Parameters.Add("MyParam", value);
 
myReportViewer.ReportSource = reportSource;
myReportViewer.RefreshReport();

The code snippet uses the Find method to get the Graph item used in the report. The modified report instance is wrapped in an InstanceReportSource object that can be used as viewer's ReportSource.

Regards,
Stef
Telerik by Progress
Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
0
Christine
Top achievements
Rank 1
answered on 09 Dec 2016, 06:06 PM

Thank you. That got me closer. I'm able to set the custom palette. However, I have 2 on the same screen and for some reason the order of colors are mixed. See attached image.

I have sorting on both set to TableNumber ASC. I even added ORDER BY TableNumber to the queries. But for some reason it seems the palette gets mixed up.

code for graphs...

var reportSource1 = new ReportsLibrary.MyChart1();
var graphItem1 = reportSource1.Items.Find("graph1", true)[0] as Graph;
graphItem1.ColorPalette = new CustomReportPalette();
reportSource1.ReportParameters["MyParam"].Value = value;
rptCurrentMonth.ReportSource = reportSource1;
rptCurrentMonth.RefreshReport();
 
var reportSource2 = new ReportsLibrary.MyChart2();
var graphItem2 = reportSource1.Items.Find("graph1", true)[0] as Graph;
graphItem2.ColorPalette = new CustomReportPalette();
reportSource2.ReportParameters["MyParam"].Value = value;
rptCurrentYear.ReportSource = reportSource2;
rptCurrentYear.RefreshReport();

 

here is the code for the palette...

public class CustomReportPalette : Telerik.Reporting.Drawing.ColorPalette
{
    public CustomReportPalette()
    {
        var colorPalette = new CustomColorPalette();
        foreach(Color color in colorPalette.Colors){
            this.Colors.Add(color);
        }
    }
}
 
public class CustomColorPalette
{
    public List<Color> Colors { get; set; }
    public CustomColorPalette()
    {
        Colors = new List<Color>();
        this.Colors.Add(Color.FromArgb(239, 117, 19));
        this.Colors.Add(Color.FromArgb(173, 161, 126));
        this.Colors.Add(Color.FromArgb(239, 209, 89));
        this.Colors.Add(Color.FromArgb(151, 160, 49));
        this.Colors.Add(Color.FromArgb(139, 198, 249));
        this.Colors.Add(Color.FromArgb(122, 108, 122));
        this.Colors.Add(Color.FromArgb(206, 66, 38));
        this.Colors.Add(Color.FromArgb(193, 202, 95));
        this.Colors.Add(Color.FromArgb(23, 69, 117));
        this.Colors.Add(Color.FromArgb(164, 193, 232));
        this.Colors.Add(Color.FromArgb(146, 72, 97));
        this.Colors.Add(Color.FromArgb(231, 182, 249));
        this.Colors.Add(Color.FromArgb(107, 67, 70));
        this.Colors.Add(Color.FromArgb(237, 238, 220));
        this.Colors.Add(Color.FromArgb(152, 176, 135));
        this.Colors.Add(Color.FromArgb(151, 107, 92));
        this.Colors.Add(Color.FromArgb(240, 161, 63));
        this.Colors.Add(Color.FromArgb(192, 72, 48));
        this.Colors.Add(Color.FromArgb(139, 71, 249));
        this.Colors.Add(Color.FromArgb(95, 79, 102));
        this.Colors.Add(Color.FromArgb(212, 114, 103));
        this.Colors.Add(Color.FromArgb(109, 209, 204));
        this.Colors.Add(Color.FromArgb(238, 232, 206));
        this.Colors.Add(Color.FromArgb(145, 126, 121));
        this.Colors.Add(Color.FromArgb(201, 44, 44));
        this.Colors.Add(Color.FromArgb(170, 136, 34));
        this.Colors.Add(Color.FromArgb(38, 61, 20));
        this.Colors.Add(Color.FromArgb(255, 231, 145));
        this.Colors.Add(Color.FromArgb(93, 122, 133));
        this.Colors.Add(Color.FromArgb(197, 86, 102));
        this.Colors.Add(Color.FromArgb(226, 192, 131));
        this.Colors.Add(Color.FromArgb(167, 252, 248));
        this.Colors.Add(Color.FromArgb(146, 161, 221));
        this.Colors.Add(Color.FromArgb(180, 163, 135));
        this.Colors.Add(Color.FromArgb(189, 201, 64));
        this.Colors.Add(Color.FromArgb(77, 75, 23));
        this.Colors.Add(Color.FromArgb(204, 102, 34));
        this.Colors.Add(Color.FromArgb(32, 198, 125));
        this.Colors.Add(Color.FromArgb(136, 133, 54));
        this.Colors.Add(Color.FromArgb(204, 136, 68));
    }
}

 

 

 

0
Accepted
Christine
Top achievements
Rank 1
answered on 09 Dec 2016, 07:39 PM

Nevermind. I see the typo now. Copy/paste graphItem2 was looking at reportSource1 when should be reportSource2.

 

Thanks

Asked by
Christine
Top achievements
Rank 1
Answers by
Stef
Telerik team
Christine
Top achievements
Rank 1
Share this question
or