C# 報表和打印等

    說到報表打印、那就不得不說須要查數據庫了,而後填寫報表信息。設計報表用的 grid++。sql

   查數據庫時候,我也是醉了,直接一個表自身與自身級聯了4次。。。一共取了7個表的信息數據。數據庫

關於級聯--(表字段相同。可是  表1的父id能夠做爲表2的子id繼續查詢,循環遞歸。。。)服務器

用了左級聯解決了,你能夠看作是一張DNA的表吧,子--父(母)--(外)祖父、(外)祖母--。。。。spa

 //級聯查詢
 
     select t1.BoarID,t1.R_Herd,t1.Sex,t1.BirthField,t1.BirthDate,t1.EarNo,t1.BirthFetus,t1.NippleOfLeft,t1.NippleOfRight,t1.R_LSize,t1.R_Coeff,t1.FatherID,t1.MotherID,
     t2.FatherID as fFatherID,t2.MotherID as fMotherID,
     t3.FatherID as ffFatherID,t3.MotherID as fmMotherID,
     t4.FatherID as fffFatherID,t4.MotherID as fmmMotherID
     m2.FatherID as MFatherID,m2.MotherID as MMotherID,     
     m3.FatherID as MfFatherID,m3.MotherID as MmMotherID,
     m4.FatherID as MffFatherID,m4.MotherID as MmmMotherID
     from  [Breed].[dbo].[MY_Y_BoarBaseInfo] t1 
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t2 on t1.FatherID = t2.BoarID 
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t3 on t2.FatherID = t3.BoarID 
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t4 on t2.MotherID = t4.BoarID 
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m2 on t1.MotherID = m2.BoarID
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m3 on m2.FatherID = m3.BoarID
     left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m4 on m2.MotherID = m4.BoarID
     where t1.BoarID = '111';

你在查詢數據的時候,首先要在數據庫寫一個存儲過程,查詢時候,直接調存儲過程便可,調用方法設計

        public DataTable selectDetailData(string str)
        {
            DataTable dt = new DataTable();
            SqlParameter[] para = {
                                      new SqlParameter("@BoarID",str)
                                  };
            string boarid = str;

            //string sqlStr = string.Format(("select FatherID,MotherID,R_Herd,EarNo,BirthDate,BirthField,NippleOfLeft,NippleOfRight,Sex,R_Count from [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = '{0}') and select FatherID as grandfatherID FROM [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = ( SELECT FatherID from [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = '{1}',"boarid,boarid);

            dt = DataBaseModule.ExecuteReturnDataTable(DataBaseModule.ConnectionString,
                    CommandType.StoredProcedure, "存儲過程名字", para);

            return dt;
        }

或者調用sql語句也可。。可是這就太。。。(注意根據使用方法選取類型)code

(關於在vs上面配置信息,就本身配吧,config裏面改下,鏈接數據庫就好了)orm

    <add key="connection" value="server=服務器;database=數據庫;user=用戶名;password=密碼;" />

一個 SQLHelper類幫助 鏈接數據庫,一句話server

 public static string ConnectionString88 = MySecurity.SDecryptString(ConfigurationManager.AppSettings.Get("connection88"));

報表設計時候、記得須要打印信息的字段填寫。。。這個要本身摸索了,上下代碼。blog

首先,報表打印的一個類。。我還沒到這一步,直接拿的別人的。。不須要的刪了遞歸

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using grdesLib;
using grproLib;

namespace GReport
{
    public class GridReport
    {

        private List<string> _list;

        public List<string> list
        {
            set { _list = value; } 
        }
        /// <summary>
        /// 報表
        /// </summary>
        GridppReport Report = new GridppReport();

        /// <summary>
        /// 須要導出的datagridview
        /// </summary>
        public DataGridView dgv;

        public DataTable db;
        public DataRow[] dr;
        /// <summary>
        /// 報表列
        /// </summary>
        public string[] ReportColumn;
        /// <summary>
        /// 報表參數
        /// </summary>
        public string[] ReportParameter;
        /// <summary>
        /// 設計程序參數
        /// </summary>
         public string[] ReportPParameter;
        /// <summary>
        /// 報表模版路徑
        /// </summary>
        public string TemplatePath;
        public object tempReport;

        /// <summary>
        /// 關聯參數
        /// </summary>
        public void Reportparameter()
        {
            if (ReportParameter.Length == ReportPParameter.Length)
            {
                for (int j = 0; j < ReportParameter.Length; j++)
                {

                    string str1 = ReportParameter[j];
                    string str2 = ReportPParameter[j];
                    Report.ParameterByName(ReportParameter[j]).AsString = ReportPParameter[j];
                }
            }
        }

        /// <summary>
        /// 填充列
        /// </summary>
        private void ReportFetchRecord()
        {
            if (dgv != null)
            {
                for (int row = 0; row < dgv.RowCount; row++)
                {
                    Report.DetailGrid.Recordset.Append();
                    for (int i = 0; i < ReportColumn.Length; i++)
                    {
                        if (dgv.Rows[row].Cells[i].Value != null)
                        {
                            Report.FieldByName(ReportColumn[i]).AsString = dgv.Rows[row].Cells[i].Value.ToString();
                            //Report.FieldByName(ReportColumn[i]).AsString = dgv.Rows[row].Cells[ReportColumn[i]].Value.ToString();
                        }
                    }
                    Report.DetailGrid.Recordset.Post();
                }
            }
            if (db != null)
            {
                for (int row = 0; row < db.Rows.Count; row++)
                {
                    Report.DetailGrid.Recordset.Append();
                    for (int i = 0; i < ReportColumn.Length; i++)
                    {
                        if (db.Rows[row][i] != null)
                        {
                            Report.FieldByName(ReportColumn[i]).AsString = db.Rows[row][i].ToString();
                            string str = db.Rows[row][i].ToString();
                        }
                    }
                    Report.DetailGrid.Recordset.Post();
                }
            }
            if (dr != null && dr.Length > 0)
            {
                for (int row = 0; row < dr.Length; row++)
                {
                    Report.DetailGrid.Recordset.Append();
                    for (int i = 0; i < ReportColumn.Length; i++)
                    {
                        if (dr[row][i] != null)
                        {
                            Report.FieldByName(ReportColumn[i]).AsString = dr[row][i].ToString();
                            string str = dr[row][i].ToString();
                        }
                    }
                    Report.DetailGrid.Recordset.Post();
                }
            }

        }
    

        private void ReportFetchRecordDB()
        {
            for (int row = 0; row < dgv.RowCount; row++)
            {
                Report.DetailGrid.Recordset.Append();
                for (int i = 0; i < ReportColumn.Length; i++)
                {
                    if (dgv.Rows[row].Cells[i].Value != null)
                    {
                        Report.FieldByName(ReportColumn[i]).AsString = db.Rows[row][i].ToString();
                    }
                }
                Report.DetailGrid.Recordset.Post();
            }
        }

        /// <summary>
        /// 去除按鈕
        /// </summary>
        public void  RemoveToolBar(GRPrintViewer pPrintViewer)
        {
            //GRPrintViewer pPrintViewer = new GRPrintViewer();
           pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExport);
           pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctMail);
           pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctSaveDocument);
           pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExportPDFBtn);
           pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExportXLSBtn);       
        }


        /// <summary>
        /// 打印預覽
        /// </summary>
        public void printpreview()
        {
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            Report.LoadFromFile(Application.StartupPath + "" + TemplatePath + "");
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            Report.ShowPreviewWnd += new _IGridppReportEvents_ShowPreviewWndEventHandler(RemoveToolBar);                                
            
            Report.PrintPreview(true);
        }

        // GridReport .Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
                   
        /// <summary>
        /// 打印預覽
        /// </summary>
        public void printpreviews()
        {
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            Report.LoadFromVariant(tempReport);
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            Report.PrintPreview(true);
        }

        /// <summary>
        /// 打印預覽
        /// </summary>
        public void printpreviews(bool flag)
        {
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            Report.LoadFromVariant(tempReport);
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);

            PrintDialog _Dialog = new PrintDialog();
            Report.Printer.PrinterName = _Dialog.PrinterSettings.PrinterName;
            Report.Print(false);
            //Report.PrintPreview(true);
        }

        /// <summary>
        /// 設置列不可見
        /// </summary>
        /// <param name="list"></param>
        private void ReportInitialize()
        {
            if (_list != null && _list.Count > 0)
            {
                foreach (string ColName in _list)
                {
                    //IGRColumn iColName = Report.ColumnByName(ColName);
                    //if (iColName != null)
                    //    iColName.Visible = false;
                    Report.ColumnByName(ColName).Visible = false;
                }
            }
        }


        /// <summary>
        /// 打印
        /// </summary>
        public void print()
        {
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            Report.LoadFromFile(Application.StartupPath + "" + TemplatePath + "");
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            Report.Print(true);
        }
    }
}

最後就是打印信息了,這幾步,本身根據需求放在合適位置,該封裝方法封裝

//下面爲打印信息

            GridReport gr = new GridReport();
           
            string[] ReportParameter = new string[] {你的字段信息(對應順序)};
                gr.ReportParameter = ReportParameter;
            string[] ReportPParameter = new string[25];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ReportPParameter[i] = string.Format("{0}", dt.Rows[0][i]);
            }
                
                gr.ReportPParameter = ReportPParameter;
                gr.TemplatePath = "/名稱.grf";
            try
                {
                    gr.printpreview();
                }
                catch
                {
                    MessageBox.Show("打印出錯了!");
                    return;
                }
            }

基本就完成了。。。可是差很少都是別人的代碼,本身也就寫了存儲過程,修修補補。。。

 
 

ALTER PROCEDURE [dbo].[MY_BoarBaseInfoStoreProc] @XXX varchar(100) //定義字段 as beginsql語句 end

相關文章
相關標籤/搜索