如何填写数据从数据集到水晶报告? [英] How to fill in data from dataset to crystal report?

查看:342
本文介绍了如何填写数据从数据集到水晶报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个空白的水晶报告

I create a blank crystal report

然后使用下面的代码,运行后看不到任何内容。

then use the following code, there is nothing to see after running.

是需要将字段对象添加到与数据集中的字段对应的crytsl报告中。
但是我不知道如何添加在这种情况下,没有通过水晶报告连接。

is it need to add field object into crytsl report corresponding to the field in data set. But i do not know how to add in this situation which is not connected through crystal report.

try
        {
            string _connectionString = ConfigurationManager.ConnectionStrings["CarParkConnectionString"].ConnectionString;

            OleDbConnection connection = null;
            try
            {
                using (connection = new OleDbConnection(_connectionString))
                {
                    //OleDbCommand command = connection.CreateCommand();
                    string selectsql = "SELECT a.Transaction_Date, a.Card_no, a.Company, a.Credit_Fee, a.Non_Credit_Fee FROM [SELECT Transaction_Date, Card_no, Company, Fee as Credit_Fee, 0 as Non_credit_fee FROM CarPark where IsCredit = true union all SELECT Transaction_Date, Card_no, Company, 0 as Credit_Fee,  Fee as Non_credit_fee FROM CarPark where IsCredit = false]. AS a where a.Transaction_Date >= " + Calendar1.SelectedDate.ToShortDateString() + " and a.Transaction_Date <= " + Calendar2.SelectedDate.ToShortDateString();
                    //command.CommandText = selectsql;
                    //SetCommandParametersForInsertUpdateTo(carpark, command, error);
                    connection.Open();

                    OleDbDataAdapter dataAdapter1 = new OleDbDataAdapter(selectsql, connection);
                    DataSet ds = new DataSet();
                    dataAdapter1.Fill(ds, "CarPark");

                    dataAdapter1.Dispose();

                    CrystalReport1 objRpt = new CrystalReport1();
                    objRpt.SetDataSource(ds.Tables[0]);

                    DailyReport_CrystalReportViewer.EnableParameterPrompt = false;
                    DailyReport_CrystalReportViewer.ReportSource = objRpt;
                    DailyReport_CrystalReportViewer.RefreshReport();
                }
            }
            catch (Exception ex)
            {
                connection.Close();
                Error_Label.Text = Error_Label.Text + " " + ex.Message;
            }
            finally
            {
                connection.Close();
            }


推荐答案

使用数据集创建水晶报表:

Here are the steps you need to create a crystal Report using dataset:

首先,此方法称为PUSH方法。

First this method is called PUSH method.

1-创建通过visual studio的数据集
转到右键单击您的项目 - >添加新项目 - >数据集调用它Dataset1

1- Create a Dataset through visual studio Go to Right click on your project --> Add new Item --> Datset Call it Dataset1

2-在您的数据集调用它(Table1)

2- Create a Table in your Dataset call it (Table1)

3-向表中添加列,指定每列的类型
假设您有2列(ID类型为int) ,(字符串的名称类型)

3- Add Columns to your table specifying the type of every column lets say you have 2 columns ( ID type of int ) , (Name type of string)

4-然后在您的报告中,您要为其选择数据源,因此在左侧的字段资源管理器中,数据库字段,右键单击它并选择数据库专家

4- then In your Report You want to Choose a datasource for it , so In your Field Explorer on the left you'll find the Database fields, right click on it and select Database expert

5-当您打开项目数据,然后ADO.NET数据集选择您的数据集(Dataset1),然后您的表(表1)

5- When you do that open project data and then ADO.NET Datasets Select your Dataset (Dataset1) and then your Table (Table1)

6-您会发现字段资源管理器中的数据库字段现在填充有表和2列id和名称。

6- You will find the Database fields in the Field Explorer is now populated with the table and the 2 columns id and name.

7-将这两个字段拖放到报告的详细信息部分中。

7- Drage and drop these two fields inside the details section of the report..

现在报告已准备就绪

8填充数据集的示例代码要查看但尚未查看,因为您需要通过数据集填充ID和名称的字段



8- Sample code to Populate the dataset

Dataset ds=new DataSet();
DataTable dt=new DataTable("Table1"); // Be sure to call this table as your Table's name
                                      // in the Dataset
dt.Columns.Add("ID", typeof(System.Integer)); //Same name of your ID column
dt.Columns.Add("Name", typeof(System.String));
Datarow dr=dt.NewRow();
dr["ID"]=1;
dr["Name"]="Test";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("Your Report's Name"));
rpt.SetDataSource(ds);

DailyReport_CrystalReportViewer.ReportSource = rpt;
DailyReport_CrystalReportViewer.DataBind();

这里是您需要的所有步骤,

Here it is all the steps you need,

希望有所帮助。

这篇关于如何填写数据从数据集到水晶报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆