Why does calculated row in dataGridView not get saved when exporting as an Excel file?

StackOverflow https://stackoverflow.com/questions/23553039

  •  18-07-2023
  •  | 
  •  

Question

The code I am working on loads an XML file into a dataTable and displays it in a dataGridView. I then average each column in the dataGridView and display them as a new row in the dataGridView. I then export the dataGridView as an Excel file, however it is does not include the added average row. Any input that is manually entered into the dataGridView does get saved in the export? I understand that with SQL databases, you must call an update on the dataGridView source. I am not quite sure what to do in this case. Any ideas on how I can get the changes to be saved?

Opens a XML file and loads it into dataGridView

Private Sub ExOpen_Click(sender As Object, e As EventArgs) Handles ExOpen.Click
' Uses a openFileDialog to find the file path for the file the user wishes to
' use. It then displays the path in a textBox. Load a DataTable with the 
' information found in the XML file and then set it as the dataSource for the 
' DataGridView()
OpenFileDialog1.FileName = Nothing
OpenFileDialog1.Filter = "XML|*.xml"
If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  lblExFile.Text = OpenFileDialog1.FileName
End If
Dim dtDataTbl As New System.Data.DataTable
Dim dsDataSet As New DataSet
For Each dtDataTbl In dsDataSet.Tables
  dtDataTbl.BeginLoadData()
Next
Try
  dsDataSet.ReadXml(lblExFile.Text)
  For Each dtDataTbl In dsDataSet.Tables
    dtDataTbl.EndLoadData()
  Next
  DataGridView1.DataSource = dtDataTbl
Catch
Finally
  dsDataSet = Nothing
  dtDataTbl = Nothing
End Try
  End Sub

Performs average of columns and adds it as a new row in dataGridView

  Private Sub ExAvg_Click(sender As Object, e As EventArgs) Handles ExAvg.Click
' Goes through each column and finds the average.  The averages are printed out
' into the DataGridView.
Dim decAvg As Decimal = 0
Dim sValue As String = Nothing
Dim sAppend As String = Nothing
Dim iCount As Integer = 0
For iColumn As Integer = 1 To DataGridView1.Columns.Count - 1
  iCount = 0
  For iRow As Integer = 0 To DataGridView1.Rows.Count - 1
    sAppend = Nothing
    If Not DataGridView1.Rows(iRow).Cells(iColumn).Value Is Nothing Then
      sValue = CStr(DataGridView1.Rows(iRow).Cells(iColumn).Value)
      Dim cChars() As Char = sValue.ToCharArray()
      ' Each character is only taken if a digit or "."  
      For Each cCharacter In cChars
        If Char.IsDigit(cCharacter) OrElse cCharacter = "." Then
          sAppend &= cCharacter
        End If
      Next
      iCount += 1
      decAvg += CDec(sAppend)
    End If
  Next
  decAvg = Math.Round(decAvg / iCount, 2)
  ' Print the average row in the DataGridView.
  Try
    If iColumn = 1 Then
      DataGridView1.Rows(iCount).Cells(iColumn).Value = "$" & CStr(decAvg)
    Else
      DataGridView1.Rows(iCount).Cells(iColumn).Value = CStr(decAvg)
    End If
    DataGridView1.Rows(iCount).Cells(0).Value = "Average"
  Catch
  End Try
Next
DataGridView1.v()
  End Sub

Exports the dataGridView as an Excel file(missing added average row)

Private Sub ExSave_Click(sender As Object, e As EventArgs) Handles ExSave.Click
' Information from the DataGridView is transferred into an excel object
' row by row and column by column.  The file name is picked by the user
' using a saveFileDialog.
SaveFileDialog1.FileName = Nothing
SaveFileDialog1.Filter = "Excel File|*.xls"
If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  lblExFile.Text = SaveFileDialog1.FileName
End If

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value

xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)

Dim dc As DataColumn
Dim dr As DataRow
Dim dt As System.Data.DataTable
dt = CType(DataGridView1.DataSource, System.Data.DataTable)
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
Try
  'Export the Columns to excel file
  For Each dc In dt.Columns
    colIndex = colIndex + 1
    xlWorkSheet.Cells(1, colIndex) = dc.ColumnName
  Next

  'Export the rows to excel file
  For Each dr In dt.Rows
    rowIndex = rowIndex + 1
    colIndex = 0
    For Each dc In dt.Columns
      colIndex = colIndex + 1
      xlWorkSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    Next
  Next

  xlWorkSheet.Columns.AutoFit()
  xlWorkBook.SaveAs(lblExFile.Text, XlFileFormat.xlWorkbookNormal, Type.Missing, _
                    Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, _
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
  xlWorkBook.Close()
Était-ce utile?

La solution

You are adding the average to the GridView but write the datatable to Excel.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top