c# – 将sql参数值传递给devexpress报告?

前端之家收集整理的这篇文章主要介绍了c# – 将sql参数值传递给devexpress报告?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将一个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;

解决方法

我想你想(1)点击按钮,(2)通过一个ID然后(3)打开报告有内容取决于该ID.所以这是我做的方式(我不知道有没有其他方式,因为devexpress不是开源):

>设计数据集,它只包含要使用Visual Studio的数据集工具显示的信息.即id,name,address,…..将该数据集命名为ReportDataset.该数据集有1个名为MyTable的表.
>使用GUI工具设计您的名为MyReport的报告(记住选择XtraReport),然后选择datasource =该ReportDataset,不要编辑GUI工具生成代码.只需使用GUI,点击&单击以添加标签,从ReportDataset添加值.
>在窗体窗体或窗体中,以下内容应在button_click事件触发的函数内(您问题的最后一个代码段中的“调用”):

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();

以上是使它更灵活,因为报告可以使用自己的数据集与不同的表的混合,….您可以通过在第1步中重用自己的数据集来更容易.希望这有帮助.

猜你在找的C#相关文章