简体   繁体   中英

How to format background of DataGridView exported to Excel?

I want to format the background of my header cells in excel, which is populated trhough my Datagridview. I´ve tried it with

ws.Cells[1. 2].Style.Fill.PatternType = ExcelFillStyle.Solid;

ws.Cells[1, 2].Style.Fill.BackgroundColor.SetColor(Color.LightGray);

but i got a error message which says the name ExcelFillStyle.Solid does not exist in the current context. Any ideas?

Thank you

Firstly welcome to the SO community as a new contributor !

Doing some research I would recommend changing the header-color of the GridView using something like this:

_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;

This was found on SO here: SO - How to change the color of dataGridView header

However If you wanted to tackle this using a more custom approach where you can set specific cells (items) background colors, this can be done using using:

DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;

The use case for this is explained on SO here:

Focus-a-specific-cell

Cell-color-changing-in-excel-using-c-sharp

Using these two sources should help you achieve what you need as these both have been answered .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM