从dgv打印信息的最佳方式

时间:2016-08-26 17:08:28

标签: c# arrays printing datagridview

我有一个dgv,我为产品添加了许多产品和代码,值等,然后我需要打印出内容。

最初,我使用此代码:

private int _Line = 0;
        void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font myFont = new Font("Courier New", 08, FontStyle.Underline, GraphicsUnit.Point);

            float lineHeight = myFont.GetHeight(e.Graphics) + 4;

            float yLineTop = e.MarginBounds.Top;



            int b = dataGridView1.Rows.Count;            
            for (int _Line = 0; _Line < b; _Line++)
            {
                if (yLineTop + lineHeight > e.MarginBounds.Bottom)
                {
                    e.HasMorePages = true;
                    return;
                }

                //e.Graphics.DrawString("TEST: " + _Line, myFont, Brushes.Black,
                // new PointF(e.MarginBounds.Left, yLineTop));

                Graphics graphics = e.Graphics;
                //Font font = new Font("Courier New", 8);
                //float fontHeight = font.GetHeight();
                int startX = 50;
                int startY = 65;
                int Offset = 40;
                graphics.DrawString("Welcome to Bakery Shop - "+DateTime.Now+".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 20;
                string underLine = "------------------------------------------";
                graphics.DrawString(underLine, new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 20;
                graphics.DrawString("" + label1.Text + "", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 20;
                graphics.DrawString("Item", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                graphics.DrawString("Cod.", new Font("Courier New", 8), new SolidBrush(Color.Black), startX+80, startY + Offset);
                graphics.DrawString("Nome", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 160, startY + Offset);
                graphics.DrawString("Valor", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 240, startY + Offset);
                graphics.DrawString("Qtd.", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 320, startY + Offset);
                graphics.DrawString("Parcial", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 400, startY + Offset);
                graphics.DrawString("Desconto", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 510, startY + Offset);
                graphics.DrawString("Subtotal", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 600, startY + Offset);
                Offset = Offset + 20;
                int a = dataGridView1.Rows.Count;
                for (int i = 0; i < a; i++)

                {

                    graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Index+1), new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 10, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 90, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[2].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 180, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[3].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 270, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[4].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 340, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[5].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 430, startY + Offset + 30);
                    graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[6].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 570, startY + Offset + 30);
                    Offset = Offset + 20;

                    graphics.DrawString("\t" +), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 570, startY + Offset + 30);
                    Offset = Offset + 20;

                }
                Offset = Offset + 20;
                Offset = Offset + 20;
                Offset = Offset + 20;
                graphics.DrawString("Total - " + textBox7.Text + ".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 20;
                graphics.DrawString("Troco - " + textBox3.Text + ".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 20;
                yLineTop += lineHeight;
            }

            e.HasMorePages = false;
        }

除了我稍后会工作的一些奇怪的行为,我注意到一行必须使每个元素很好地对齐,以便它不会在信息之上打印信息。

然后我想,如果我转换完整行并在每个单元格之间添加空格并打印数组,它将永远不会显示信息高于信息,即使例如&#34;代码&#34;太大了,所以它不会在&#34;名称&#34;上面显示/混合列。

这是更好的方法吗?我该如何开始制作呢?因为我看到的每个地方,解决方案是将单个单元格作为字符串传递。

就我而言,每一行都有6列,非常感谢!

2 个答案:

答案 0 :(得分:0)

修正如下:

int a = dataGridView1.Rows.Count;                
                for (int i = 0; i < a; i++)               

                {
                    string[] dgvtoarray = { Convert.ToString(dataGridView1.Rows[i].Index + 1), Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), Convert.ToString(dataGridView1.Rows[i].Cells[2].Value), Convert.ToString(dataGridView1.Rows[i].Cells[3].Value), Convert.ToString(dataGridView1.Rows[i].Cells[4].Value), Convert.ToString(dataGridView1.Rows[i].Cells[5].Value), Convert.ToString(dataGridView1.Rows[i].Cells[6].Value) };
                    var result = string.Join(" | ", dgvtoarray);

                    graphics.DrawString(Convert.ToString(result), new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset + 30);
                    Offset = Offset + 20;

                }

答案 1 :(得分:0)

从datagridview打印的最佳方法是将数据传输到Excel工作表。

        Microsoft.Office.Interop.Excel.Worksheet ws;
        try
        {
            Microsoft.Office.Interop.Excel.Application Excell = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb = Excell.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
            ws = (Microsoft.Office.Interop.Excel.Worksheet)Excell.ActiveSheet;
            Excell.Visible = true;
        }
        catch (Exception)
        {
            return;
        }

        int i = 1;

        foreach (DataGridViewColumn clm in dgw.Columns)
        {
            ws.Cells[2, i] = clm.Name;
            Microsoft.Office.Interop.Excel.Range xcell = ws.Cells[2, i];
            Microsoft.Office.Interop.Excel.Borders brd1 = xcell.Borders;
            xcell.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            brd1.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            brd1.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium;
            i++;
        }

        Microsoft.Office.Interop.Excel.Range row = ws.Cells[2, i];
        row.EntireRow.Font.Bold = true;

        Microsoft.Office.Interop.Excel.Range fcell = ws.Cells[1, 1];
        Microsoft.Office.Interop.Excel.Range lcell = ws.Cells[1, i - 1];
        Microsoft.Office.Interop.Excel.Range space = ws.get_Range(fcell, lcell);
        space.Merge(true);
        space.EntireRow.Font.Bold = true;

        ws.Cells[1, 1] = //Your text here
        Microsoft.Office.Interop.Excel.Borders brd = space.Borders;
        aralik.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

        int m = 3;

        foreach (DataGridViewRow rows in dgw.Rows)
        {
            for (int p = 1; p < i; p++)
            {
                if (rows.Cells[p-1].Value.ToString()=="")
                {
                    string str = string.Empty;

                    foreach (Control item in cList)
                    {
                        if (item.Name=="lbl_"+(rows.Index.ToString())+"_"+((p-1).ToString()))
                        {
                            str = item.Text;
                            ws.Cells[m, p] = str;
                        }
                    }
                }
                else
                {
                    ws.Cells[m, p] = rows.Cells[p - 1].Value;
                }
            }

            m++;
        }

        ws.Columns.AutoFit();

这是我在学校考试申请中的代码。希望这会有所帮助。