- Home
- Forum
- ASP.NET Core - EJ 2
- Not able to change the Background of a single cell in a Syncfusion.Pdf.Grid.PdfGrid
Not able to change the Background of a single cell in a Syncfusion.Pdf.Grid.PdfGrid
Hallo,
I'm able to change the background of a complete row in a PdfGrid with this code:
var pdfGridRow = pdfGrid.Rows.Add();
pdfGridRow.Style.BackgroundBrush = PdfBrushes.LightYellow;
Now I only want to change the background of a cell in the row when it has a value. To do so I use this code but it does not work:
for (int ColumnIndex = 0; ColumnIndex <= 2; ColumnIndex++)
{
if (pdfGridRow.Cells[ColumnIndex].Value != "")
{
pdfGridRow.Cells[ColumnIndex].Style.BackgroundBrush =
PdfBrushes.LightYellow;
}
}
Do you have a sample to only change the background of a single cell in a row?
Hi Vince,
To change the background of a single cell in a `PdfGrid` row only when it has a value, you can utilize the `BeginCellLayout` event. Here's a sample code snippet demonstrating how to achieve this:
|
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Grid; using System.Drawing; using System.IO;
namespace Create_PDF { public class Program { public static void Main(string[] args) { // Create a new PDF document PdfDocument document = new PdfDocument(); PdfPage page = document.Pages.Add();
// Create a new PdfGrid PdfGrid pdfGrid = new PdfGrid(); pdfGrid.Columns.Add(3);
// Add a row PdfGridRow pdfGridRow = pdfGrid.Rows.Add();
// Handle the BeginCellLayout event pdfGrid.BeginCellLayout += (sender, eventArgs) => { var cellValue = pdfGridRow.Cells[eventArgs.CellIndex].Value?.ToString(); if (!string.IsNullOrEmpty(cellValue)) { // Change background of the cell eventArgs.Style.BackgroundBrush = PdfBrushes.LightYellow; } };
// Example to set values in cells for testing pdfGridRow.Cells[0].Value = "Value 1"; pdfGridRow.Cells[1].Value = ""; // An empty cell pdfGridRow.Cells[2].Value = "Value 3";
// Draw the grid pdfGrid.Draw(page, new PointF(10, 10));
// Stream the document to disk using (FileStream stream = new FileStream("RtlPdfGrid.pdf", FileMode.Create, FileAccess.Write)) { document.Save(stream); } } } }
|
### Explanation:
- BeginCellLayout Event: This event allows you to apply styling specifically to cells during their layout process. By checking the cell value, you can decide whether to change its background color.
- Conditional Styling: The condition `if (!string.IsNullOrEmpty(cellValue))` ensures that only cells with a value have their background changed.
Follow the below links for more information:
Please integrate this sample into your application and let us know the outcome. This approach should allow you to change the background of individual cells efficiently.
Regards,
Irfana J
Hallo,
Thanks for the reply.
Your sample is working when the code line "var cellValue = pdfGridRow.Cells[eventArgs.CellIndex].Value?.ToString();" is changed in "var cellValue = eventArgs.Value?.ToString();".
Hi Vince,
Thank you for sharing your feedback regarding the code modification. We're glad to hear that the sample is working with your change. Please let us know if you have any further queries or need additional assistance.
Regards,
Irfana J.
- 3 Replies
- 2 Participants
-
VI Vince
- Apr 10, 2025 03:48 PM UTC
- Apr 23, 2025 10:14 AM UTC