from dgv to excel start on line 18

I'm able to print from dgv to excel with the following.

Dim r As Integer

Dim c As Integer

For r = 0 To InvoiceDetailsDataGridView.Rows.Count - 1

For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

Dim s As String = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

xlsheet.Cells(r + 1, c + 1).Value = s

Next

But how can I get it to start in excel line 18



Answer this question

from dgv to excel start on line 18

  • a.d.m

    Hi,

    Change>>

    For r = 0 To InvoiceDetailsDataGridView.Rows.Count - 1

    to

    For r = 17 To InvoiceDetailsDataGridView.Rows.Count - 1

     

    as counting starts at zero the 17th r value is actually the 18th down the page ( if you think about it ) as i see you are doing r + 1 .

     

    Regards,

    S_DS

     



  • Devesh Dwivedi

    ok I got it,

    Dim r As Integer

    Dim c As Integer = 1

    Dim rc As Integer = 18

    For r = 0 To InvoiceDetailsDataGridView.Rows.Count - 1

    For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

    Dim s As String = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

    xlsheet.Cells(rc, c + 1).value = s

    Next

    rc = rc + 1

    Next


  • Leo Liu

    Dim r As Integer

    Dim c As Integer

    Dim s As String

    For r = 17 To InvoiceDetailsDataGridView.Rows.Count - 1

    For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

    = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

    xlsheet.Cells(r + 1, c + 1).Value = s

    Next

     

    The above is what i was meaning, you only had to change one line.

    Read a FOR NEXT loop as follows.

    FOR ( every value of ) r = startValue To theEndValue

    ' do this bit of code

    NEXT r ' get the next value of r until theEndValue is reached.

     

    Make the computer count 50 to 100>>

    For r= 50 to 100

    Next r

    Make the computer do the same backwards>>

    For r = 100 to 50 Step -1

    Next r

    A FOR NEXT loop doesn't have to start at 0 or 1.

    It is better to keep a DIM statement outside of a FOR NEXT loop too.

    Are you sure you want to Dim s As a String and have the values rewritten to EXCEL cells as a string

    Why not

    Dim s As Integer

     

    Regards,

    S_DS

     



  • Dongwei

    Dim r As Integer

    Dim c As Integer = 1

    Dim rc As Integer = 18

    For r = rc To InvoiceDetailsDataGridView.Rows.Count - 1

    For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

    Dim s As String = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

    xlsheet.Cells(r, c + 1).value = s

    Next

    Next

    The above should work too or set DIm rc As Integer = 17 as that is row 18.

    Your last post looked more complicated.

    Regards,

    S_DS



  • Twyford

    I understood what you said, when I do

    Dim r As Integer

    Dim c As Integer

    Dim s As String

    For r = 17 To InvoiceDetailsDataGridView.Rows.Count - 1

    For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

    s = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

    xlsheet.Cells(r + 1, c + 1).Value = s

    Next

    I get nothing, it just opens my excel file.


  • avenger_219

    Ok, that didn't work for some reason, but if I do this

    Dim r As Integer

    Dim c As Integer = 1

    Dim rc As Integer = 18

    For r = 0 To InvoiceDetailsDataGridView.Rows.Count - 1

    For c = 0 To InvoiceDetailsDataGridView.Rows(r).Cells.Count - 1

    Dim s As String = InvoiceDetailsDataGridView.Rows(r).Cells(c).Value

    xlsheet.Cells(rc, c + 1).value = s

    Next

    Next

    It writes the row to the excel sheet but immediatly erases it ( basically it flashes)

    Any ideas


  • from dgv to excel start on line 18