Method's type signature is not Interop compatible. [Copying DataGridValues to excel cell object]]
I have this original snippet which copies datagridview values into the microsoft excel sheet:
The code above throws an exception of method type signature is not interop compatible. I was debugging and checking MSDN classes on Workbook. When I debugged at adding cell values, i did catch the exception.
The solution is simple. Just add ToString() after the value, since value is of type object.
for (int i = 0; i < dataGridViewObjects[gcount].Columns.Count; i++) { sheet.Cells[1, i + 1] = dataGridViewObjects[gcount].Columns[i].Name; for (int row = 0; row < dataGridViewObjects[gcount].Rows.Count; row++) { for (int column = 0; column < dataGridViewObjects[gcount].Columns.Count; column++) { sheet.Cells[row + 2, column + 1] =dataGridViewObjects[gcount].Rows[row].Cells[column].Value; } } }
The solution is simple. Just add ToString() after the value, since value is of type object.
sheet.Cells[row + 2, column + 1] = dataGridViewObjects[gcount].Rows[row].Cells[column].Value.ToString();
Brilliant! That saved me a few hours.
ReplyDeleteThanks a lot.... :D
Deletebut when i converted value to Value.ToString(); the date format has changed. in some part it showing in dd/MM/YYY format and in next row it shows mm/dd/yyyy format how to solve this
ReplyDeleteHi,
DeleteSimply use String.Format() with Iformatprovider for Date formatting. Or, convert the object value to date time object using Convert.TodateTime() method.