How can I apply currency style to a specific column on a pdfgrid with a datasource

I'm having issues trying to format a column to currency style when it comes from a data source because I'm not filling the pdfgrid manually to apply Format("data", "C"), I need to do something like pdfgrid.Column(1).style = "C" or something that helps me with it.


problema1.jpg  

The first pdfgrid is the one with the problem I need the last four columns as currency but the code for that grid is:

 pdfGrid3.DataSource = PARTIDAS


The second one where the Total is i could format it but it is because i made the data like this:

 Dim pdfGrid As New PdfGrid()

 Dim dataTable As New DataTable()


 dataTable.Columns.Add("SUBTOTAL")

 dataTable.Columns.Add(Format(SPARTIDAS.Rows(0).Item(0), "C"))

 dataTable.Rows.Add(New Object() {"IVA", Format(SPARTIDAS.Rows(0).Item(1), "C")})

 dataTable.Rows.Add(New Object() {"TOTAL", Format(SPARTIDAS.Rows(0).Item(0) + SPARTIDAS.Rows(0).Item(1), "C")})

 pdfGrid.DataSource = dataTable


Any ideas??, thanks in advance


1 Reply

IJ Irfana Jaffer Sadhik Syncfusion Team August 19, 2025 09:34 AM UTC

Hi Federico,

When you're binding a PdfGrid directly to a DataTable using pdfGrid.DataSource = PARTIDAS, you don't have direct control over formatting individual cells during data binding. However, you can still apply formatting after the grid is populated by iterating through the rows and applying styles to specific columns.

Here’s a complete example using Syncfusion's PDF library that demonstrates how to format the last four columns (PRECIO, SUBTOTAL, IVA, TOTAL) as currency:

        // Create a new PDF document

        PdfDocument document = new PdfDocument();

        PdfPage page = document.Pages.Add();

 

        // Create and populate the DataTable

        DataTable dataTable = new DataTable();

        dataTable.Columns.Add("CANTIDAD");

        dataTable.Columns.Add("CLAVE");

        dataTable.Columns.Add("DESCRIPCION");

        dataTable.Columns.Add("PRECIO");

        dataTable.Columns.Add("SUBTOTAL");

        dataTable.Columns.Add("IVA");

        dataTable.Columns.Add("TOTAL");

 

        dataTable.Rows.Add("1", "R1", "PRODUCTO 1", 150, 150, 24, 174);

        dataTable.Rows.Add("1", "C1", "CABLES PARA LOS EQUIPOS", 250, 250, 40, 290);

 

        // Create the PdfGrid and assign the data source

        PdfGrid pdfGrid = new PdfGrid();

        pdfGrid.DataSource = dataTable;

 

        // Style header row

        PdfSolidBrush headerBrush = new PdfSolidBrush(Color.LightGray);

        foreach (PdfGridCell cell in pdfGrid.Headers[0].Cells)

        {

            cell.Style.BackgroundBrush = headerBrush;

            cell.Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);

        }

 

        // Format currency columns after data binding

        foreach (PdfGridRow row in pdfGrid.Rows)

        {

            for (int i = 3; i <= 6; i++) // Columns: PRECIO, SUBTOTAL, IVA, TOTAL

            {

                if (decimal.TryParse(row.Cells[i].Value.ToString(), out decimal value))

                {

                    row.Cells[i].Value = string.Format("{0:C}", value);

                    row.Cells[i].Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);

                }

            }

        }

 

        // Draw the main grid

        pdfGrid.Draw(page, new PointF(10, 10));

 

        // Create summary table

        DataTable summaryTable = new DataTable();

        summaryTable.Columns.Add("Label");

        summaryTable.Columns.Add("Value");

 

        summaryTable.Rows.Add("SUBTOTAL", string.Format("{0:C}", 400));

        summaryTable.Rows.Add("IVA", string.Format("{0:C}", 64));

        summaryTable.Rows.Add("TOTAL", string.Format("{0:C}", 464));

 

        PdfGrid summaryGrid = new PdfGrid();

        summaryGrid.DataSource = summaryTable;

 

        // Style summary table

        foreach (PdfGridRow row in summaryGrid.Rows)

        {

            row.Cells[0].Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);

            row.Cells[1].Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);

        }

 

        // Draw summary grid below the main grid

        float yOffset = 10 + pdfGrid.Rows.Count * 20 + 50;

        summaryGrid.Draw(page, new RectangleF(50, yOffset, 200, 100));

 

        // Save the document

        document.Save("InvoiceGrid.pdf");

        document.Close(true);

 



Key Steps:

  1. Bind the DataTable to the PdfGrid.
  2. Iterate through rows and format specific columns using string.Format("{0:C}", value).
  3. Apply right alignment using PdfStringFormat(PdfTextAlignment.Right) for currency columns.


This approach gives you full control over formatting even when using a data source. Let me know if you'd like help adapting this to your specific dataset or layout!


Follow the link below for more details:

String Formatting to the Column | Syncfusion UG documentation


Regards,

Irfana J.



Loader.
Up arrow icon