C# 水晶报表打印 绑定数据库表

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

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

你可能感兴趣的:(数据库)