vb.net - Excel Export exporting same row multiple times -
i've written export procedure vb.net application, exports data displayed in datagridview
into excel spreadsheet.
however, instead of copying 23 of rows in datagridview
, copies same one, 23 times. wrong below for loop
that means copies 1 record?
for = 0 dgvexport.rowcount - 1 j = 0 dgvexport.columncount - 1 k integer = 1 dgvexport.columns.count xlworksheet.cells(1, k) = dgvexport.columns(k - 1).headertext xlworksheet.cells(i + 2, j + 1) = dgvexport(j, 1).value.tostring next next next
i need field names copied, hence headertext
line , k
variable.
a snippet of dgv, show there different records;
the resultant export;
edit
as suggested, changed code following
k integer = 1 dgvexport.columns.count xlworksheet.cells(1, k) = dgvexport.columns(k - 1).headertext next = 0 dgvexport.rowcount - 1 j = 0 dgvexport.columncount - 1 xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring next next
but error
object reference not set instance of object
on line xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring
the original problem of duplicate rows this:
for = 0 dgvexport.rowcount - 1 j = 0 dgvexport.columncount - 1 k integer = 1 dgvexport.columns.count
the cells being iterated many times. use 1 loop export headertext, data.
object reference not set instance of object
aka nullreferenceexception
if allowusertoaddrows
true on dgv, means looping 1 many rows. cells in row nothing
, nullreferenceexception
:
= 0 dgvexport.rowcount - 1 if dgvexport.rows(i).isnewrow continue j = 0 dgvexport.columncount - 1 xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring next next
or change row loop:
' 1 less account new user row = 0 dgvexport.rowcount - 2
Comments
Post a Comment