Reputation: 23
I want export to datagrid to wpf but when export an error is generated
An unhandled exception of type 'System.NullReferenceException' occurred in solutions.exe
my code...
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Workbook workbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
Worksheet sheet1 = (Worksheet)workbook.Sheets[1];
Range range;
Range myRange;
for (int i = 1; i < dgDatos.Columns.Count; i++)
{
range = (Range)sheet1.Cells[1, i + 1];
sheet1.Cells[1, i + 1].Font.Bold = true;
range.Value = dgDatos.Columns[i].Header;
for (int j = 0; j < dgDatos.Items.Count; j++)
{
TextBlock b = dgDatos.Columns[i].GetCellContent(dgDatos.Items[j]) as TextBlock;
myRange = sheet1.Cells[j + 2, i + 1];
myRange.Value = b.Text;
}
}
}
Upvotes: 1
Views: 2176
Reputation: 1129
Without giving any indication of where the null value is, my bet would be on
myRange.Value = b.Text;
When you do this:
TextBlock b = dgDatos.Columns[i].GetCellContent(dgDatos.Items[j]) as TextBlock;
... b
will be null if the object assigned to that cell is not a TextBlock
.
For that matter, why have a TextBlock
in the DataGrid
? Check out DataTable
.
Upvotes: 1