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

namespace 手动分页测试_C版_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void myp_HeaderFooterOut(int pages, int curpage)
        {
            string mys ;
            mys = "第" + Convert.ToString(curpage) + "页 总共" + Convert.ToString(pages) + "页";
            Font myfont = new Font("宋体", 12);
            myp.SetCurrentPage(curpage);
            myp.Currentx = 0;
            myp.Currenty = 0;

            myp.DrawText(mys, myp.PaperPrintWidth, 50, myfont, Color.Red, StringAlignment.Far, StringAlignment.Far, false, false, false,
            false, 0, 0);

        }

        private void Button3_Click(object sender, EventArgs e)
        {
           
            try
            {
                int num = 0;
                num = Convert.ToInt32(tablerows.Text);
                if (num <= 0)
                {
                    return;
                }
                dgv1.RowCount = num;
                for (int i = 0; i <= dgv1.RowCount - 1; i++)
                {
                    dgv1.Rows[i].Cells[0].Value = Convert.ToString(i + 1);
                }
                Random myrnd = new Random();

                for ( int i = 0; i <= dgv1.RowCount - 1; i++)
                {
                    for (int j = 1; j <= dgv1.ColumnCount - 2; j++)
                    {
                        dgv1.Rows[i].Cells[j].Value = "第" + Convert.ToString(i + 1) + "行第" + Convert.ToString(j) + "列";
                    }
                    dgv1.Rows[i].Cells[dgv1.ColumnCount - 1].Value = myrnd.Next(100);
                }

            }
            catch (Exception ex)
            {
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button3_Click(null, null);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            myp.NewDoc();
            DataGridView mydgv = new DataGridView();
            myp.CopyDataGridView(dgv1, mydgv);
            float sumrowheight = 0;
            //合计行要打印的行高
            sumrowheight = myp.ConvFromDisplay(mydgv.Rows[0].Height, false);
            bool isneednewpage = true;
            int startrow = 0;
            int endrow = 0;
            startrow = 0;
            for (int i = 0; i <= mydgv.RowCount - 1; i++)
            {
                if (isneednewpage == true)
                {
                    myp.NewPage();
                    DrawTitle();
                    myp.DrawDGVHeader(mydgv, true, true, "1111", 0);
                    isneednewpage = false;
                }
                if (myp.IsNewPage(myp.ConvFromDisplay(mydgv.Rows[i].Height, false)) == true)
                {
                    //打印合计数
                    endrow = i - 1;
                    PrintSumRow(mydgv, startrow, endrow, sumrowheight, "本页小计");
                    startrow = i;
                    isneednewpage = true;
                    i = i - 1;
                }
                else
                {
                    myp.DrawDGVRow(mydgv, i, true, false, "1111", 0);
                }
            }


            PrintSumRow(mydgv, 0, mydgv.RowCount - 1, sumrowheight, "总计");

            myp.EndDoc("打印预览");

        }
        private void PrintSumRow(DataGridView dgv, int startrow, int endrow, float rowheight, string ss)
        {
            decimal je = default(decimal);
            je = 0;
            for (int i = startrow; i <= endrow; i++)
            {
                je = je + Convert.ToDecimal(dgv.Rows[i].Cells[6].Value);
            }
            string mys = null;
            mys = ss + ":" + Convert.ToString(je);
            myp.PrintFooter("", mys, "", new Font("黑体", 12, FontStyle.Bold));
        }

        private void DrawTitle()
        {
            Font myfont = new Font("黑体", 18, FontStyle.Bold);
            string mytitle = "这是表格的标题";
            myp.DrawTitle(mytitle, myfont, Color.Blue, 0);
            myp.NewRow(myp.LastDrawTextHeight);

        }

    }
}
