Donate

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:
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 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.
sheet.Cells[row + 2, column + 1] = dataGridViewObjects[gcount].Rows[row].Cells[column].Value.ToString();

Comments

  1. Brilliant! That saved me a few hours.

    ReplyDelete
  2. but 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

    ReplyDelete
    Replies
    1. Hi,

      Simply use String.Format() with Iformatprovider for Date formatting. Or, convert the object value to date time object using Convert.TodateTime() method.

      Delete

Post a Comment

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.