I've been looking through the code and have found a bunch of coding errors. My bad!
First, for this to work properly the DataGrid needs to have columns. Which you've added already.
Next, we need to revise the code for the last combobox.
Dim row As New DataGridViewRow 'The row object
Dim cell As DataGridViewCell = New DataGridViewTextBoxCell 'The columns
cell.Value = ComboBox1.GetItemText(ComboBox1.SelectedItem)
row.Cells.Add(cell)
cell = New DataGridViewTextBoxCell
cell.Value = ComboBox2.GetItemText(ComboBox2.SelectedItem)
row.Cells.Add(cell)
cell = New DataGridViewTextBoxCell
cell.Value = ComboBox3.GetItemText(ComboBox3.SelectedItem)
row.Cells.Add(cell)
cell = New DataGridViewTextBoxCell
cell.Value = ComboBox4.GetItemText(ComboBox4.SelectedItem)
row.Cells.Add(cell)
' And so on
DataGridView1.Rows.Add(row)
We also need to add two lines to the SaveToFile method:
Private Sub SaveToFile(ByVal fileName As String)
If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
Dim sw As New System.IO.StreamWriter(stream)
For Each row As DataGridViewRow In DataGridView1.Rows
' This makes sure that only rows with data in it gets read.
If row.IsNewRow = False Then
Dim line As String
line = row.Cells(0).Value.ToString()
line &= ";" & row.Cells(1).Value.ToString()
line &= ";" & row.Cells(2).Value.ToString()
line &= ";" & row.Cells(3).Value.ToString()
line &= ";" & row.Cells(4).Value.ToString()
line &= ";" & row.Cells(5).Value.ToString()
line &= ";" & row.Cells(6).Value.ToString()
line &= ";" & row.Cells(7).Value.ToString()
' This line was missing and also why the file was empty
sw.WriteLine(line)
End If
Next
sw.Flush()
sw.Close()
End If
End Sub
As for the form closing code.
The way you have coded it will always ask whether or not you would like to save.
Add …