C# 使用字符串中的datagridview值进行打印

C# 使用字符串中的datagridview值进行打印,c#,ms-access,printing,C#,Ms Access,Printing,我创建了一个POS系统,在销售表单中,用户将产品添加到datagridview,然后将其保存到数据库中 我目前正在使保存按钮也打印收据,但我想将此datagridview项目添加到收据中,但显然我做错了 这就是打印字符串 string welcome = "Thank You For Visiting Dulabk"; string InvoiceNo = txtInvoiceNo.Text; string InvoiceDate = dateTimePicker1.Value.ToLongDa

我创建了一个POS系统,在销售表单中,用户将产品添加到datagridview,然后将其保存到数据库中

我目前正在使保存按钮也打印收据,但我想将此datagridview项目添加到收据中,但显然我做错了

这就是打印字符串

string welcome = "Thank You For Visiting Dulabk";
string InvoiceNo = txtInvoiceNo.Text;
string InvoiceDate = dateTimePicker1.Value.ToLongDateString();
string list = dataGridView1.Rows[i].Cells[1].Value, dataGridView1.Rows[i].Cells[3].Value, dataGridView1.Rows[i].Cells[0].Value;

Bitmap bitm = new Bitmap(list.Length * 30, 90);

            using (Graphics graphic = Graphics.FromImage(bitm))
因此,我认为datagridview值应该写得不同

我使用for语句作为数据库输入

for (int i = 0; i<dataGridView1.Rows.Count; i++ )
        {
            OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TS-POS.accdb");
            OleDbCommand cmd = new OleDbCommand("INSERT INTO Sales (InvoiceNo,InvoiceDate, Pname, Pprice, Bcode, Staff) VALUES ('" + txtInvoiceNo.Text + "','" + dateTimePicker1.Value + "', '" + dataGridView1.Rows[i].Cells[1].Value + "', '" + dataGridView1.Rows[i].Cells[3].Value + "', '" + dataGridView1.Rows[i].Cells[0].Value + "', '" + lblUser.Text + "')", conn);
           // cmd.Parameters.AddWithValue("@staff", OleDbType.VarChar).Value = lblUser.Text;
            conn.Open();
            cmd.ExecuteNonQuery(); 
            conn.Close();

        }

用于(int i=0;iIs这是您的完整代码吗?我根本看不到您在位图中写入任何内容。始终使用参数以避免sql注入和格式错误。这是您想要的吗?我用“尝试我仔细思考”编辑了该问题。您正在各行循环,并且可能希望在y中的单独一行上打印每个项目我们的图形。但是你总是在同一个点上打印。在这个循环中的某个地方,你需要增加你的“Y”位置,使之成为上一行下方的x像素量。
 private void printready()
        {


            stri

ng welcome = "Thank You For Visiting Dulabk";
            string InvoiceNo = txtInvoiceNo.Text;
            string InvoiceDate = dateTimePicker1.Value.ToLongDateString();
            Bitmap bitm = new Bitmap(welcome.Length * 30, 90);

            using (Graphics graphic = Graphics.FromImage(bitm))
            {


                Font newfont = new Font("Arial Black", 8);
                Font newfont2 = new Font("Arial Black", 10);
                PointF point = new PointF(40f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString("" + InvoiceNo + "", newfont2, black, point);
                PointF pointPrice = new PointF(15f, 45f);
                graphic.DrawString("" + InvoiceDate + "", newfont2, black, pointPrice);
                PointF pointPname = new PointF(10f, 65f); 
                PointF pointBar = new PointF(10f, 65f);
            graphic.DrawString("" + welcome + "", newfont2, black, pointBar);
            PointF pointList = new PointF(10f, 65f); 

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

                    graphic.DrawString("  " + dataGridView1.Rows[i].Cells[0].Value +"" + dataGridView1.Rows[i].Cells[1].Value +"" + dataGridView1.Rows[i].Cells[2].Value +"" , new Font("Arial Bold", 11),
                             new SolidBrush(Color.Black), pointList);
                                    }

                PointF pointItemEnd = new PointF(10f, 65f);
                graphic.DrawString(" ---------- ", newfont2, black, pointItemEnd);

            }


            using (MemoryStream Mmst = new MemoryStream())
            {


                bitm.Save("ms", ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;


            }