C#从SQL结果中打印多个页面

时间:2017-02-27 14:13:32

标签: c# sql sql-server printing

假设我有一个包含20行的表,我想在每页上打印5个员工姓名。共4页。

我当前代码的问题是,它尝试在第一页上显示每个员工姓名。另外,它以无限循环结束。 这是我的代码:

DataTable dt = new DataTable();

private void FillDataTable()
    {
        SqlConnection sqlConnection = GetConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;

        string query =
        @"SELECT * FROM EMPLOYEES ORDER BY ID";

        cmd.CommandText = query;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection;

        sqlConnection.Open();

        reader = cmd.ExecuteReader();

        if (reader.HasRows)
        {
            dt.Load(reader);
        }
        else
        {
            Console.WriteLine("No rows found.");

        }
        reader.Close();
        sqlConnection.Close();
    }

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {         
        foreach(DataRow row in dt.Rows)
        {     
            e.Graphics.DrawString(row[4].ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, y, new StringFormat());

            y += 100;

            if (itemperpage < 5)
            {
                itemperpage += 1;
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;

            }

        }                       
    }

private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        y = 80;
        itemperpage = 0;
    }

我在这里缺少什么?如何在每个页面上只显示5个员工姓名,直到完成SQL结果表?

2 个答案:

答案 0 :(得分:1)

您需要手动跟踪页数,因为页面信息仅包含描述页面的属性,而不是打印过程。然后,您可以将DataTable转换为IEnumerable并使用Ling Skip()和Take()扩展方法来简化编码。使用缩小的集合强制转换回数据表将允许您使用切除的代码进行遍历。

    using System.Linq;
    ...

    private int _CurentPageNumber = 0;

    private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        y = 80;
        itemperpage = 0;
        _CurentPageNumber = 0;
    }

    private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        _CurentPageNumber++;

        int skip = (_CurentPageNumber - 1) * itemperpage;

        var query = dt.AsEnumerable().Skip(skip).Take(itemperpage);

        foreach (DataRow row in query.CopyToDataTable().Rows)
        {
            e.Graphics.DrawString(row[4].ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, y, new StringFormat());
        }
    }

答案 1 :(得分:0)

我编辑了@Ross Bush Code,现在似乎有效。

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        neturalDataSet.centersDataTable table = new neturalDataSet.centersDataTable();

        centersTableAdapter.Fill(table);

        y = 80;

        itemperpage = 0;


        _CurrentPageNumber++;

        if(_CurrentPageNumber != 1)
        {
            skip += 6;
        }

        var query = table.AsEnumerable().Skip(skip);

        foreach (DataRow row in query.CopyToDataTable().Rows)
        {
            e.Graphics.DrawString(row[0].ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, y, new StringFormat());

            y += 100;

            if (itemperpage < 5)
            {
                itemperpage += 1;
                e.HasMorePages = false;

            }
            else
            {
                e.HasMorePages = true;
                return;
            }

        }

    }