Articles in this section
Category / Section

How to set custom text for column header in WPF Spreadsheet?

3 mins read

WPF Spreadsheet (SfSpreadsheet) does not contain direct support to set custom text for the column header. This can be achieved by overriding the OnRender method and customizing the SpreadsheetHeaderCellRenderer.

// Event subscription
spreadsheet.WorkbookLoaded += OnWorkbookLoaded; 

// Event customization
private void OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
{
   // Remove the default header cell renderer
   spreadsheet.ActiveGrid.CellRenderers.Remove("Header");
   // Add the custom header cell renderer
   spreadsheet.ActiveGrid.CellRenderers.Add("Header",new CustomHeaderCellRenderer());
}  

// Custom header cell renderer
public class CustomHeaderCellRenderer : SpreadsheetHeaderCellRenderer
{

   // Override the OnRender method
   protected override void OnRender(RowColumnIndex cellRowColumnIndex, DrawingContext dc, Rect cellRect, SpreadsheetColumn column, object textElement)
   {

       // Customize based on your scenario
       if(column.DisplayText == "A")
       {
           // Change the Header Text for A column
           column.DisplayText = "Item Name";  
       } 

       if (column.DisplayText == "B")
       {
           // Change the Header Text for B column
           column.DisplayText = "Quantity";
       } 

       if (column.DisplayText == "C")
       {
           // Change the Header Text for C column
           column.DisplayText = "Price";
       } 

       if (column.DisplayText == "D")
       {
           // Change the Header Text for D column
           column.DisplayText = "Amount";
       } 

       if (column.DisplayText == "E")
       {
           // Change the Header Text for E column
           column.DisplayText = "Discount";
       } 

       if (column.DisplayText == "F")
       {
           // Change the Header Text for F column
           column.DisplayText = "Profit";
       } 

       base.OnRender(cellRowColumnIndex, dc, cellRect, column, textElement);
   }
}

The following screenshot illustrates the custom text for the column header in Spreadsheet.

Shows the custom header text in the column header of SfSpreadsheet

Take a moment to peruse the WPF Spreadsheet - Getting Started documentation, where you can find about the spreadsheet with code examples.

View sample in GitHub.

Conclusion

I hope you enjoyed learning about how to set custom text for column header in Spreadsheet.

You can refer to our WPF Spreadsheet feature tour page to know about its other groundbreaking feature representations and WPF Spreadsheet documentation, how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied