将 SQL 参数值传递给 DevExpress 报表?

10

我想从用户表单将一个SQL参数传递给我的报告类,但它无法工作,不会创建报告,当我在将ID参数添加到报告类后再次打开报告设计器选项卡时,它会刷新报告并删除我的组件。

问题出在哪里?

这是我的报告类:

public SodoorZemanatName(long ID)
    {
        InitializeComponent(ID);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Designer generated code
    private void InitializeComponent(long ID)
    {
            this.components = new System.ComponentModel.Container();
            DevExpress.DataAccess.Sql.CustomSqlQuery customSqlQuery1 = new DevExpress.DataAccess.Sql.CustomSqlQuery();
            DevExpress.DataAccess.Sql.QueryParameter queryParameter1 = new DevExpress.DataAccess.Sql.QueryParameter();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SodoorZemanatName));
            this.topMarginBand1 = new DevExpress.XtraReports.UI.TopMarginBand();
            this.detailBand1 = new DevExpress.XtraReports.UI.DetailBand();
            this.bottomMarginBand1 = new DevExpress.XtraReports.UI.BottomMarginBand();
            this.sqlDataSource2 = new DevExpress.DataAccess.Sql.SqlDataSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.topMarginBand1.HeightF = 100F;
            this.topMarginBand1.Name = "topMarginBand1";
            this.detailBand1.HeightF = 100F;
            this.detailBand1.Name = "detailBand1";
            this.bottomMarginBand1.HeightF = 100F;
            this.bottomMarginBand1.Name = "bottomMarginBand1";
            this.sqlDataSource2.ConnectionName = "Context";
            this.sqlDataSource2.Name = "sqlDataSource2";
            customSqlQuery1.Name = "Query";
            queryParameter1.Name = "ID";
            queryParameter1.Type = typeof(long);
            queryParameter1.ValueInfo = "0";
            queryParameter1.Value = ID;
            customSqlQuery1.Parameters.Add(queryParameter1);
            customSqlQuery1.Sql = "select * from LG_Garanti where ID=@ID";
            this.sqlDataSource2.Queries.AddRange(new DevExpress.DataAccess.Sql.SqlQuery[] {
            customSqlQuery1});
            this.sqlDataSource2.ResultSchemaSerializable = resources.GetString("sqlDataSource2.ResultSchemaSerializable");
    this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] {
    this.topMarginBand1,
    this.detailBand1,
    this.bottomMarginBand1});
    this.ComponentStorage.AddRange(new System.ComponentModel.IComponent[] {
    this.sqlDataSource2});
    this.DataSource = this.sqlDataSource2;
    this.Version = "15.2";
    ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
}
#endregion

这是我的呼唤:

SodoorZemanatName report = new SodoorZemanatName(1);
ASPxDocumentViewer1.ReportTypeName = "SodoorZemanatName";
ASPxDocumentViewer1.Report = report;

我认为你需要单独设计报告。 仅仅使用 new DevExpress.XtraReports.UI.XtraReport(); 是不够的,因为它不知道如何使用你的数据源,如何展示数据。DevExpress.XtraReports.UI.XtraReport report = new your_report_design_class();在过去,我曾经使用过这种方式,在分配数据源之前必须先创建一个带有我的设计的新报告。 - minhhn2910
1个回答

1

我猜你想要(1)点击按钮,(2)传递一个ID,然后(3)打开报告内容取决于该ID。所以这就是我做的方式,(我不确定是否有其他方式,因为devexpress不是开源的):

  1. Design your dataset, it contains only the info you want to show using the dataset tool of Visual Studio. i.e. id, name, address, ..... Name that dataset = ReportDataset. That dataset has 1 table named MyTable.
  2. Design your report named MyReport using the GUI tool (remember to choose XtraReport), and select the datasource = that ReportDataset, do not edit the code generated by the GUI tool. Just use the GUI, click & click to add labels, add value from the ReportDataset.
  3. In you form, window form or whatever, the below should be inside the function triggered by button_click event (your "calling" in the last snippet of your question):

    DataSet new_ds = new DataSet();
    ReportDataset.MyTable runtime_data = new ReportDataset.MyTable();
    //get data from your database according to ID, Row by row 
    //then add them to the runtime_data table
    //in your case, add all the result of "select * from LG_Garanti where ID=@ID";
    runtime_data.Rows.Add(new object[] {.blah blah..});// just add row, use whatever method you like
    new_ds.Tables.Add(runtime_data);
    //from this point, new_ds contains runtime data of the row(s) you want ID.
    //now just link that dynamic dataset to your designed report
    MyReport my_report = new MyReport();
    my_report.DataSource = new_ds;
    // Show the print preview or do whatever you want 
    ReportPrintTool printTool = new ReportPrintTool(my_report);
    printTool.ShowRibbonPreviewDialog();
    

上述方法是为了使报告更加灵活,因为报告可以使用其自己的数据集并混合不同的表格.....您可以通过在第一步中重复使用自己的数据集来使其更容易。希望这能帮到您。


在我的代码后台中,我无法访问ReportDataset,因为我已经在报表设计器中创建了它。 - Hamid Reza
抱歉造成困惑,请在外部创建数据源,然后将其分配给您的报表(使用.xsd文件)。我现在无法访问Visual Studio,因此无法确定。https://documentation.devexpress.com/#XtraReports/CustomDocument4797 - minhhn2910
创建数据集并将其保存为.xsd文件,如下所示:https://msdn.microsoft.com/zh-cn/library/ms171897.aspx - minhhn2910
我认为他需要这些文档:https://documentation.devexpress.com/#XtraReports/CustomDocument5210 和 https://documentation.devexpress.com/#XtraReports/CustomDocument17387。 - bbb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接