从dgv中打印信息的最佳方式

本文关键字:最佳 方式 信息 打印 dgv | 更新日期: 2023-09-27 18:04:26

我有一个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;
        }

除了一些奇怪的行为,我将稍后工作,我注意到,单行必须有每个元素对齐好,这样,它不会打印信息上面的信息。

然后我想,如果我转换完整行并在每个单元格之间添加空格,并打印数组,它将永远不会在info上面显示info,即使作为示例"code"太大,所以它不会显示在上面/与"name"列混合。

这是一个更好的方法吗?我怎么开始做呢?因为我看到的每个地方,解决方案都是将单个单元格作为字符串传递。

在我的情况下,每行,将有6列,非常感谢提前!

从dgv中打印信息的最佳方式

修复如下:

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

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

这是我从一个学校考试应用程序的代码。