Change column header text alignment and cell color

Hello,

I am writing a table to a pdf and I need to align the column header text alignment and cell color. How can I do this?

Thanks,

Caleb 

3 Replies

SL Sowmiya Loganathan Syncfusion Team February 22, 2018 02:46 PM UTC

Hi Caleb, 

Thank you for contacting Syncfusion support. 

Please find the below code snippet to align the column header text alignment in PdfGrid 
            //Create a new PdfGrid. 
            PdfGrid pdfGrid = new PdfGrid();   
 
            //Create and customize string format  
            PdfStringFormat format = new PdfStringFormat(); 
            format.Alignment = PdfTextAlignment.Center; 
 
            //Specify the style for PdfGridcell 
            PdfGridCellStyle headerstyle = new PdfGridCellStyle(); 
            headerstyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); 
            headerstyle.StringFormat = format; 
 
            //Apply style 
            pdfGrid.Headers.ApplyStyle(headerstyle); 
  
Please find the below code snippet to apply cell color in PdfGrid 

            //Create a new PdfGrid. 
            PdfGrid pdfGrid = new PdfGrid();   
 
            //Specify the style for PdfGridcell 
            PdfGridCellStyle cellStyle = new PdfGridCellStyle(); 
            cellStyle.BackgroundBrush = PdfBrushes.Pink; 
 
            //Apply style for rows 
            pdfGrid.Rows.ApplyStyle(cellStyle); 
 
            //Apply style for specific cell 
            pdfGrid.Rows[1].Cells[1].Style = cellStyle; 

We have created the sample to draw table in PDF document. Please find the sample from the below link: 

Please try the above sample in your end and let us know if it meets your requirement. If not, please let us know further details regarding your requirement. It will be helpful for us to provide a precise solution. 

Regards, 
Sowmiya L  



MA Martin June 3, 2021 10:23 AM UTC

Does this actually answer the question ?

I want to change the background color of individual header cells not the entire header (i.e. column 3 header red, column 4 header green).

I changed this to 

pdfGrid.Headers[0].Cells[2].Style = headerStyleRed
pdfGrid.Headers[0].Cells[3].Style = headerStyleGreen


Which did the trick


GK Gowthamraj Kumar Syncfusion Team June 4, 2021 11:52 AM UTC

Hi Martin, 
 
Thank you for your update.

In our earlier update, we have shared the code details for setting background color for entire grid cell. Yes, we can set the background color of individual cells with the code snippet which you shared in your update.
 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon