I'm trying to export my data from a dataGridView to an excel file. It works like charm, but I just want to have the name of the columns too. I wrote the following code, but I can't get it with this error. Thanks !
Error: The type or namespace name 'SqlCe' could not be found (are you missing a using directive or an assembly reference?)
Code:
foreach (SqlCe.Data.DataColumn dc in dataGridView1.Columns)
colNames[col++] = dc.ColumnName;
char lastColumn = (char)(65 + dataGridView1.Columns.Count - 1);
xlWorkSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
xlWorkSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
xlWorkSheet.get_Range("A1", lastColumn + "1").VerticalAlignment= Excel.XlVAlign.xlVAlignCenter;
The reference for SQL Server CE: using System.Data.SqlServerCe;
There is no type called SqlCe
as far as I can see... for the DataColumn
type, you should just be using System.Windows.Forms.DataGridViewColumn
, ideally via a using
directive for System.Windows.Forms
. Note that DataGridViewColumn
doesn't have a ColumnName
property, but it does have a Name
property:
using System.Windows.Forms;
...
foreach (DataGridViewColumn dc in dataGridView1.Columns)
{
colNames[col++] = dc.Name;
}
There's no need to refer to SqlCe
there, because a DataGridView
isn't provider-specific.
Also note that if colNames
just contains these column names, you could use:
var colNames = dataGridView1.Columns
.Cast<DataGridViewColumn>()
.Select(c => c.Name)
.ToArray(); // Or ToList
See more on this question at Stackoverflow