引用一些代码,没想那么多,只是留作参考,以后不用再拿本本来回找了而已。

public string UserName = string.Empty;
        public string Number = string.Empty;
        /// <summary>
        /// 水晶报表一
        /// </summary>
        private void InitRept()
        {
            try
            {
                DataTable dt = new DataTable("PZhTable");
                dt.Columns.Add("Name");
                dt.Columns.Add("Number");
                dt.Columns.Add("Name1");
                dt.Columns.Add("Number1");

                DataRow NewRow = dt.NewRow();
                NewRow["Name"] = UserName;
                NewRow["Number"] = Number;
                NewRow["Name1"] = UserName;
                NewRow["Number1"] = Number;
                dt.Rows.Add(NewRow);
                ReportDocument rptc = new ReportDocument();
                string rptFile = Application.StartupPath + "\\Rept.rpt";
                rptc.Load(rptFile);
                rptc.Database.Tables[0].SetDataSource(dt);
                this.crystalReportViewer1.ReportSource = rptc;
                crystalReportViewer1.PrintReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

          //调用和传值

           Rept.frmRept frm = new Rept.frmRept();
            frm.UserName = UserName;
            frm.Number = Convert.ToInt32(lblNumber.Text.Trim()).ToString();
            frm.ShowDialog();

        /// <summary>
        /// 水晶报表二
        /// </summary>
        private void InitRept()
        {
            try
            {
                DataTable dt = new DataTable("ZiGeTable");
                dt.Columns.Add("Name");
                dt.Columns.Add("ZiGeHao");
                dt.Columns.Add("Name1");
                dt.Columns.Add("ZiGeHao1");
                dt.Columns.Add("Name2");
                dt.Columns.Add("ZiGeHao2");
                string MySql = "SELECT * from ZiGeTable order by ShunXuHao asc";
                DataTable newdt = new DataAccess().GetDataSet(MySql).Tables[0];
                if (newdt != null && newdt.Rows.Count > 0)
                {
                    for (int i = 0; i < newdt.Rows.Count; i++)
                    {
                        DataRow NewRow = dt.NewRow();
                        if (i < newdt.Rows.Count - 1)
                        {
                            NewRow["Name"] = newdt.Rows[i]["ClientInfoID"].ToString();
                            NewRow["ZiGeHao"] = newdt.Rows[i]["ShunXuHao"].ToString();
                        }
                        if (i < newdt.Rows.Count - 1)
                        {
                            NewRow["Name1"] = newdt.Rows[i + 1]["ClientInfoID"].ToString();
                            NewRow["ZiGeHao1"] = newdt.Rows[i + 1]["ShunXuHao"].ToString();
                        }
                        if (i < newdt.Rows.Count - 2)
                        {
                            NewRow["Name2"] = newdt.Rows[i + 2]["ClientInfoID"].ToString();
                            NewRow["ZiGeHao2"] = newdt.Rows[i + 2]["ShunXuHao"].ToString();
                        }
                        i = i + 2;
                        dt.Rows.Add(NewRow);
                    }

                    ReportDocument rptc = new ReportDocument();
                    string rptFile = Application.StartupPath + "\\ReptXH.rpt";
                    rptc.Load(rptFile);
                    rptc.Database.Tables[0].SetDataSource(dt);
                    this.crystalReportViewer1.ReportSource = rptc;

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

posted on 2010-03-20 12:52  GT_Andy  阅读(1246)  评论(0编辑  收藏  举报