2012-03-15 81 views
1

使用C sharp,winforms,visual studio 2010,.NET 4和devexpress API。C#Devexpress,更新報告

嗨,我最近試圖使用devexpress xtreport控件進行報告。 它工作正常,看起來不錯,唯一的問題是我需要它在運行時更新。

我遇到的問題是當我的控件更新時,我需要它們將值傳遞給我的報告,到目前爲止,這不工作,也不是我的報告xtrachart,沒有更新。

如果有這樣的方法,我正在徘徊我應該調用更新整個報告。

原因我正在使用文本框值放入報表是由於報表的顯示不會改變,事實上我正在使用少量的數據在我的Winforms應用程序中得到處理。

繼承人一些代碼:

public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport 
{ 
    DataTable chartTable3 = new DataTable("Table1"); 
    DataTable chartTable4 = new DataTable("Table2"); 
    DataTable chartTable5 = new DataTable("Table3"); 
    DataTable chartTable6 = new DataTable("Table4"); 
    public DevExpress.XtraReports.UI.XRChart xrChart1; 

    public XtraReport1() 
    { 
     InitializeComponent(); 

    } 

    public void branchname(string bname) 
    { 
     branch.Text = bname; 
    } 


    public void names(string pg1k, string pg2k, string pg3k, string pg4k) 
    { 
     Pg1.Text = pg1k; 
     Pg2.Text = pg2k; 
     Pg3.Text = pg3k; 
     Pg4.Text = pg4k; 
    } 

    public void TotalSalesk(string totS) 
    { 
     Sales.Text = totS; 
    } 

    public void TotalQtyk(string totQ) 
    { 
     Qty.Text = totQ; 
    } 

    public void SelectedSales(string pg1S, string pg2S, string pg3S, string pg4S) 
    { 
     //xrLabel1.Text = label1; 
     Pg1Sales.Text = pg1S; 
     Pg2Sales.Text = pg2S; 
     Pg3Sales.Text = pg3S; 
     Pg4Sales.Text = pg4S; 
    } 

    public void SelectedQty(string pg1Q, string pg2Q, string pg3Q, string pg4Q) 
    { 
     Pg1Qty.Text = pg1Q; 
     Pg2Qty.Text = pg2Q; 
     Pg3Qty.Text = pg3Q; 
     Pg4Qty.Text = pg4Q; 
    } 

    public void PgGrpPercent(string pg1P, string pg2P, string pg3P, string pg4P) 
    { 
     Pg1Perc.Text = pg1P; 
     Pg2Perc.Text = pg2P; 
     Pg3perc.Text = pg3P; 
     Pg4perc.Text = pg4P; 
    } 

    public void PgTop50(string pg1t5, string pg2t5, string pg3t5, string pg4t5) 
    { 
     Pg1T50.Text = pg1t5; 
     Pg2T50.Text = pg2t5; 
     Pg3T50.Text = pg3t5; 
     Pg4T50.Text = pg4t5; 
    } 

    public void PgTop100(string pg1t1, string pg2t1, string pg3t1, string pg4t1) 
    { 
     Pg1T100.Text = pg1t1; 
     Pg2T100.Text = pg2t1; 
     Pg3T100.Text = pg3t1; 
     Pg4T100.Text = pg4t1; 
    } 

    public void PgTop200(string pg1t2, string pg2t2, string pg3t2, string pg4t2) 
    { 
     Pg1T200.Text = pg1t2; 
     Pg2T200.Text = pg2t2; 
     Pg3T200.Text = pg3t2; 
     Pg4T200.Text = pg4t2; 
    } 

    public void PgOver(string pg1tO, string pg2tO, string pg3tO, string pg4tO) 
    { 
     Pg1Over.Text = pg1tO; 
     Pg2over.Text = pg2tO; 
     Pg3over.Text = pg3tO; 
     Pg4over.Text = pg4tO; 
    } 

    public void CheckClear(bool checkme) 
    { 
     if (checkme == true) 
     { 
      //DevExpress.XtraReports.UI.XRChart xrChart1; 
      chartTable3.Clear(); 
      chartTable3.Columns.Clear(); 
      chartTable4.Clear(); 
      chartTable4.Columns.Clear(); 
      chartTable5.Clear(); 
      chartTable5.Columns.Clear(); 
      chartTable6.Clear(); 
      chartTable6.Columns.Clear(); 
     } 
    } 

    public void myChartSeries1(double top, double tmid, double bmid, double bottom, string pgName, bool table) 
    { 
     //xrChart1.Series.Clear(); 


      // Add two columns to the table. 
      chartTable3.Columns.Add("Names", typeof(string)); 
      chartTable3.Columns.Add("Value", typeof(Int32)); 
      chartTable3.Rows.Add("Below 50", top); 
      chartTable3.Rows.Add("Between 50-100", tmid); 
      chartTable3.Rows.Add("Between 100-200", bmid); 
      chartTable3.Rows.Add("Greater than 200", bottom); 

      Series series3 = new Series(pgName, ViewType.Bar); 

      //.Series.Add(series1); 
      series3.DataSource = chartTable3; 

      series3.ArgumentScaleType = ScaleType.Qualitative; 

      series3.ArgumentDataMember = "Names"; //error here 

      series3.ValueScaleType = ScaleType.Numerical; 

      series3.ValueDataMembers.AddRange(new string[] { "Value" }); 

      //series1.Label.PointOptions.PointView = PointView.ArgumentAndValues; 

      series3.LegendPointOptions.PointView = PointView.ArgumentAndValues; 

      series3.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; 

      series3.LegendPointOptions.ValueNumericOptions.Precision = 0; 
      xrChart1.Series.Add(series3); 

      xrChart1.Legend.Visible = true; 


    } 

    public void myChartSeries2(double top, double tmid, double bmid, double bottom, string pgName, bool table) 
    { 

      // Add two columns to the table. 
      chartTable4.Columns.Add("Names", typeof(string)); 
      chartTable4.Columns.Add("Value", typeof(Int32)); 
      chartTable4.Rows.Add("Below 50", top); 
      chartTable4.Rows.Add("Between 50-100", tmid); 
      chartTable4.Rows.Add("Between 100-200", bmid); 
      chartTable4.Rows.Add("Greater than 200", bottom); 

      Series series4 = new Series(pgName, ViewType.Bar); 

      //.Series.Add(series1); 
      series4.DataSource = chartTable4; 

      series4.ArgumentScaleType = ScaleType.Qualitative; 

      series4.ArgumentDataMember = "Names"; 

      series4.ValueScaleType = ScaleType.Numerical; 

      series4.ValueDataMembers.AddRange(new string[] { "Value" }); 

      //series1.Label.PointOptions.PointView = PointView.ArgumentAndValues; 

      series4.LegendPointOptions.PointView = PointView.ArgumentAndValues; 

      series4.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; 

      series4.LegendPointOptions.ValueNumericOptions.Precision = 0; 
      xrChart1.Series.Add(series4); 

      xrChart1.Legend.Visible = true;   
    } 

    public void myChartSeries3(double top, double tmid, double bmid, double bottom, string pgName, bool table) 
    { 

      // Add two columns to the table. 
      chartTable5.Columns.Add("Names", typeof(string)); 
      chartTable5.Columns.Add("Value", typeof(Int32)); 
      chartTable5.Rows.Add("Below 50", top); 
      chartTable5.Rows.Add("Between 50-100", tmid); 
      chartTable5.Rows.Add("Between 100-200", bmid); 
      chartTable5.Rows.Add("Greater than 200", bottom); 

      Series series5 = new Series(pgName, ViewType.Bar); 

      //.Series.Add(series1); 
      series5.DataSource = chartTable5; 

      series5.ArgumentScaleType = ScaleType.Qualitative; 

      series5.ArgumentDataMember = "Names"; 

      series5.ValueScaleType = ScaleType.Numerical; 

      series5.ValueDataMembers.AddRange(new string[] { "Value" }); 

      //series1.Label.PointOptions.PointView = PointView.ArgumentAndValues; 

      series5.LegendPointOptions.PointView = PointView.ArgumentAndValues; 

      series5.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; 

      series5.LegendPointOptions.ValueNumericOptions.Precision = 0; 
      xrChart1.Series.Add(series5); 

      xrChart1.Legend.Visible = true; 


    } 

    public void myChartSeries4(double top, double tmid, double bmid, double bottom, string pgName, bool table) 
    { 

      // Add two columns to the table. 
      chartTable6.Columns.Add("Names", typeof(string)); 
      chartTable6.Columns.Add("Value", typeof(Int32)); 
      chartTable6.Rows.Add("Below 50", top); 
      chartTable6.Rows.Add("Between 50-100", tmid); 
      chartTable6.Rows.Add("Between 100-200", bmid); 
      chartTable6.Rows.Add("Greater than 200", bottom); 

      Series series6 = new Series(pgName, ViewType.Bar); 

      //.Series.Add(series1); 
      series6.DataSource = chartTable6; 

      series6.ArgumentScaleType = ScaleType.Qualitative; 

      series6.ArgumentDataMember = "Names"; 

      series6.ValueScaleType = ScaleType.Numerical; 

      series6.ValueDataMembers.AddRange(new string[] { "Value" }); 

      //series1.Label.PointOptions.PointView = PointView.ArgumentAndValues; 

      series6.LegendPointOptions.PointView = PointView.ArgumentAndValues; 

      series6.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent; 

      series6.LegendPointOptions.ValueNumericOptions.Precision = 0; 
      xrChart1.Series.Add(series6); 

      xrChart1.Legend.Visible = true; 


    } 
} 

我意識到這個代碼需要大量的清理工作,因爲它可以做了很多整潔短,但這是暫時的測試,如果我能得到的報告工作。 也請原諒方法和變量與壞名字,這些被映射到我的設計作爲我的參考,並且當這完成時也將被改變,我可以想出一些好名字。

非常感謝。我的報告

副本

enter image description here

@reniuz

namespace RepSalesNetAnalysis 
{ 
public partial class MyPrintPreviewForm : DevExpress.XtraEditors.XtraForm 
{ 
    public MyPrintPreviewForm() 
    { 
     InitializeComponent(); 
    } 

    XtraReport1 report = new XtraReport1(); 

    private void MyPrintPreviewForm_Load(object sender, EventArgs e) 
    { 
     printControl1.PrintingSystem = report.PrintingSystem; 
     //creating document 
     report.CreateDocument(); 
    } 

    public void addSeries1(double top, double tmid, double bmid, double bottom, string pgName, bool table1) 
    { 
     //calling your method in report 
     report.myChartSeries1(top, tmid, bmid, bottom, pgName, table1); 
     //recreate document 
     //report.CreateDocument(); 
    } 

    public void addSeries2(double top, double tmid, double bmid, double bottom, string pgName, bool table2) 
    { 
     //calling your method in report 
     report.myChartSeries2(top, tmid, bmid, bottom, pgName, table2); 
     //recreate document 
     //report.CreateDocument(); 
    } 

    public void addSeries3(double top, double tmid, double bmid, double bottom, string pgName, bool table3) 
    { 
     //calling your method in report 
     report.myChartSeries3(top, tmid, bmid, bottom, pgName, table3); 
     //recreate document 
     //report.CreateDocument(); 
    } 

    public void addSeries4(double top, double tmid, double bmid, double bottom, string pgName, bool table4) 
    { 
     //calling your method in report 
     report.myChartSeries4(top, tmid, bmid, bottom, pgName, table4); 
     //recreate document 
     //report.CreateDocument(); 
    } 

    public void addBranchName(string BranchName) 
    { 
     report.branchname(BranchName); 
    } 

    public void addNames(string pg1k, string pg2k, string pg3k, string pg4k) 
    { 
     report.names(pg1k, pg2k, pg3k, pg4k); 
    } 

    public void addTotalSalesk(string totS) 
    { 
     report.TotalSalesk(totS); 
    } 

    public void addTotalQtyk(string totQ) 
    { 
     report.TotalQtyk(totQ); 
    } 

    public void addSelectedSales(string pg1S, string pg2S, string pg3S, string pg4S) 
    { 
     report.SelectedSales(pg1S, pg2S, pg3S, pg4S); 
    } 

    public void addSelectedQty(string pg1Q, string pg2Q, string pg3Q, string pg4Q) 
    { 
     report.SelectedQty(pg1Q, pg2Q, pg3Q, pg4Q); 
    } 

    public void addPgGrpPercent(string pg1P, string pg2P, string pg3P, string pg4P) 
    { 
     report.PgGrpPercent(pg1P, pg2P, pg3P, pg4P); 
    } 

    public void addPgTop50(string pg1t5, string pg2t5, string pg3t5, string pg4t5) 
    { 
     report.PgTop50(pg1t5, pg2t5, pg3t5, pg4t5); 
    } 

    public void addPgTop100(string pg1t1, string pg2t1, string pg3t1, string pg4t1) 
    { 
     report.PgTop100(pg1t1, pg2t1, pg3t1, pg4t1); 
    } 

    public void addpgTop200(string pg1t2, string pg2t2, string pg3t2, string pg4t2) 
    { 
     report.PgTop200(pg1t2, pg2t2, pg3t2, pg4t2); 
    } 

    public void addPgOver(string pg1tO, string pg2tO, string pg3tO, string pg4tO) 
    { 
     report.PgOver(pg1tO, pg2tO, pg3tO, pg4tO,); 
    } 

} 
} 

我應該在哪裏把創建文件?

+0

的DevExpress報告有顯示報告時,可輸入參數。也許你應該使用它們,而不是自定義輸入 – Reniuz 2012-03-15 09:18:16

+0

你的意思是顯示報告時的用戶輸入?就像我說過的我有文本框顯示我的表格中的計算結果。然後,我將它們顯示的值作爲報表的輸入。硬編碼分配給我需要的確切佈局。這會降低動態性,但我知道報表從不會更改值,因此當我的文本框更新時,我還需要更新報表框以及xtrachart的信息。這需要發生在我預覽我的報告或預覽 – lemunk 2012-03-15 09:59:28

+0

你有什麼意思嗎? – lemunk 2012-03-15 10:01:25

回答

2

代碼更新報告:

//XtraReport1 is your report 
XtraReport1 report = new XtraReport1(); 
//Open print preview form 
report.ShowPreview(); 

//Update your report content 
report.myChartSeries2(1,2,3,4,DateTime.Now.ToShortTimeString()); 
//Recreate/update document in print preview 
report.CreateDocument(); 

有關CreateDocument()方法訪問更多的細節和注意事項DevExpress documentation

+0

theres有一個小問題,當我關閉printpreview窗體,然後再次運行按鈕,它拋出異常錯誤。 set {printControl1.PrintingSystem = value.PrintingSystem;它說這需要一個「新」關鍵字來創建對象 – lemunk 2012-03-15 12:32:00

+1

好吧,我愚蠢:)你甚至不需要任何自定義窗體。當你使用'XtraReport1 report = new XtraReport1(); report.ShowPreview();'當你需要刷新報表時調用'report.CreateDocument();'所以在這個問題的答案你的問題的答案使用CreateDocument刷新它:)...第二杯咖啡大腦開始後工作:) – Reniuz 2012-03-15 12:44:46

+0

lmao,所以我需要的一切都是那一行哈哈 – lemunk 2012-03-15 12:46:21