Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add Barcode in last column of my grid for each row.

I am using Itext sharp.

When I try, it adds in every cell of grid. I need to define how to add only in last cell in a row (or in cell [12])

Thanks.

What I have tried:

C#
//Adding DataRow
           foreach (DataGridViewRow row in OperacijeDataGridView.Rows)
           {
               foreach (DataGridViewCell cell in row.Cells)
               {
                   PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                   cell2.HorizontalAlignment = Element.ALIGN_LEFT;
                   cell2.FixedHeight = 20f;

                   Barcode128 code128 = new Barcode128();
                   code128.CodeType = Barcode.CODE128_RAW;
                   code128.ChecksumText = true;
                   code128.GenerateChecksum = true;
                   code128.Code = Barcode128.GetRawText(row.Cells[12].Value.ToString(), false, Barcode128.Barcode128CodeSet.AUTO);
                   System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
                   iTextSharp.text.Image barCode = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Png);
                   //PdfPCell pdfCell = new PdfPCell();
                   //PdfPTable tmpTable = new PdfPTable(1);
                   //tmpTable.WidthPercentage = 100;
                   PdfPCell tmpCell = new PdfPCell(barCode);
                   //tmpTable.AddCell(new Paragraph(row.Cells[12].Value.ToString().ToUpper(), calibri));
                   barCode.ScaleAbsolute(60, 20); //100x40
                   tmpCell.FixedHeight = 30;//60
                   tmpCell.HorizontalAlignment = Element.ALIGN_CENTER;
                   tmpCell.VerticalAlignment = Element.ALIGN_MIDDLE;
                   tmpCell.BorderWidth = 0;
                   //tmpTable.AddCell(tmpCell);
                   // tmpTable.DefaultCell.BorderWidth = 0;
                   // tmpTable.DefaultCell.VerticalAlignment = Element.ALIGN_TOP;
                   // tmpTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                   //tmpTable.AddCell(new Paragraph(row.Cells[4].Value.ToString().ToUpper(), calibri6));
                   //tmpTable.AddCell(new Paragraph(row.Cells[12].Value.ToString().ToUpper(), calibri));
                   // pdfCell.AddElement(tmpTable);
                   // tmpCell.AddElement(tmpTable);
                   pdfTable.AddCell(tmpCell);

                   pdfTable.AddCell(cell2);
               }
           }
Posted
Updated 11-Jul-20 22:25pm
v2
Comments
Laxmidhar tatwa technologies 13-Jul-20 11:54am    
The last cell should be DataGridViewImageCell

iTextSharp.text.Image barCode = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Png);
cell.Value = barcode;

1 solution

Then don't add a cell (or actually two) each time round your columns loop ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900