Click here to Skip to main content
15,305,361 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
         If HdInventoryDataGridView.RowCount = Nothing Then
            MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf & "Please retrieve data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        Dim rowsTotal, colsTotal As Short
        Dim I, j, iC As Short
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        Dim xlApp As New Excel.Application
        Try
            Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            xlApp.Visible = True

            rowsTotal = HdInventoryDataGridView.RowCount - 1
            colsTotal = HdInventoryDataGridView.Columns.Count - 1
            With excelWorksheet
                .Cells.Select()
                .Cells.Delete()
                For iC = 0 To colsTotal
                    .Cells(1, iC + 1).Value = HdInventoryDataGridView.Columns(iC).HeaderText
                Next
                For I = 0 To rowsTotal - 1
                    For j = 0 To colsTotal
                        .Cells(I + 2, j + 1).value = HdInventoryDataGridView.Rows(I).Cells(j).Value
                    Next j
                Next I
                .Rows("1:1").Font.FontStyle = "Bold"
                .Rows("1:1").Font.Size = 12

                .Cells.Columns.AutoFit()
                .Cells.Select()
                .Cells.EntireColumn.AutoFit()
                .Cells(1, 1).Select()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            'RELEASE ALLOACTED RESOURCES
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing
        End Try
    End Sub
Posted
Updated 8-Apr-15 20:56pm
v3

1 solution

As I recall, the Cells collection indexes start at 1. Hence, I suspect that you are missing the last row because your limit is count - 1.
   
Comments
Maciej Los 9-Apr-15 3:00am
   
Are you sure? See: DataGridView.RowCount Property[^] and DataGridView.ColumnCount Property [^]
It starts from zero to count minus one...
veena15 9-Apr-15 3:12am
   
Can i know where i must change my code ??
David A. Gray 9-Apr-15 17:01pm
   
The fact that setting it to zero causes all rows to be deleted makes it abundantly clear to me that rows are counted from 1. Eliminate the "-1" from bot of your limit definitions, and I'll bet that you get all rows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900