I noticed that when I export to Excel from a DGV, the export leaves out cells to the right and below an empty cell (with Nothing or Null in it), i.e., terminates at that cell. I am using a dbNull trap, so is there something I am missing?
Code:Dim dgv As DataGridView = CType(TabControl2.TabPages(1).Controls("dgv1"), DataGridView) Dim dt As New DataTable() 'Adding the Columns For Each column As DataGridViewColumn In dgv.Columns dt.Columns.Add(column.HeaderText, column.ValueType) Next 'Adding the Rows Try For Each row As DataGridViewRow In dgv.Rows dt.Rows.Add() For Each cell As DataGridViewCell In row.Cells If cell.Value IsNot Nothing Then dt.Rows(dt.Rows.Count - 1)(cell.ColumnIndex) = cell.Value.ToString() End If If IsDBNull(cell.Value) Then dt.Rows(dt.Rows.Count - 1)(cell.ColumnIndex) = "" End If Next Next Catch End Try 'Exporting to Excel Try Using wb As New XLWorkbook() wb.Worksheets.Add(dt, RunName) wb.SaveAs(SaveFilename) wb.Dispose() End Using dt.Dispose() Catch System.Windows.Forms.MessageBox.Show("You have the the export file open in Excel. Please close the file, and then retry exporting.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End Try




Reply With Quote
