Setting Excel range font etc styles using C#

Hi everybody...

I am developing a Windows application in C# to automate excel. I push rows from datagridview to the excel sheet. I have some cells in datagridview that haveBold font, and can be inItalicsalso. How can set the excel cells font style (bold or italics) the same as it is in datagridview.

I tried this and it is giving an error:

Code Block

((Range)objSheet.Cells[sheetRowIndex, 1]).Font.Bold = dataGridViewPricing.Rows[sheetRowIndex - 12].Cells["PartNumber"].Style.Font.Bold;

The error:

Object reference not set to an instance of an object.

Thanks.

Bullpit

[981 byte] By [bullpit] at [2008-1-10]
# 1

I think I got it. I am adding rows to the datagridview thru a button click and also setting font style while adding. I don't set font style for all rows, just for headers of each group (the rows are grouped). So it throws an exception when the DefaultCellStyle.Font property of the row is not set. For now, I just use a dummy try catch block to ignore this exception. Is there a better way to do this. I tried setting the columns DefaultCellStyle property from the designer, but it did not help.

bullpit at 2007-10-3 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...