HELP! Trying to save data from an unbound datagridview control to a table

AnthonyJ20

New member
Joined
Apr 20, 2012
Messages
1
Programming Experience
1-3
Hey everyone, I'm somewhat new to visual basic which is why I'm hoping someone can help me understand how to save data to a table created in visual studio from an unbound datagridview control in a form. The data in the gridview control is populated from a text file I import and want to save to the table. Below is the code I'm using. As you can see, after I import and read the file I'm trying to add this data to a table I created locally in Visual Studio. The table is called FlatFileImport. I'm trying to save the records to the table using the FlatFileImportTableAdapter.update but it's not working.

Any thoughts? :unhappy:



VB.NET:
If IsDBNull(Me.FileData.Text) Or Len(Me.FileData.Text) < 1 Then
MsgBox("You must select the reformatter flate file", MsgBoxStyle.Information)
Exit Sub
Else
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser(Me.FileData.Text)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
FlatFileImportDataGridView.DataSource = Nothing
Try
currentRow = MyReader.ReadFields()
FlatFileImportDataGridView.Rows.Add(currentRow)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
End Using
'FlatFileImportDataGridView.DataSource = FlatFileImportBindingSource
Dim dr As New System.Windows.Forms.DataGridViewRow
For Each dr In Me.FlatFileImportDataGridView.SelectedRows
'Me.DataGridView2.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value, dr.Cells(3).Value, dr.Cells(4).Value, dr.Cells(5).Value, dr.Cells(6).Value, dr.Cells(7).Value)
Next
For Each row1 As DataGridViewRow In FlatFileImportDataGridView.Rows
Dim copyrow As DataRow = Me.Database1DataSet.FlatFileImport.NewRow
For Each col As DataGridViewCell In row1.Cells
copyrow(col.Value) = (col.Value)
Next
Me.Database1DataSet.FlatFileImport.Rows.Add(copyrow)
Next
Me.FlatFileImportTableAdapter.Update(Me.Database1DataSet.FlatFileImport)
End If
 
It doesn't really make sense to use an unbound grid. If you're going to put the data in a DataTable to save it anyway then why not put it in the DataTable first and then bind that to the grid?

As for the question, there are only three possibilities when you call Update on an adapter:

1. The call succeeds and returns zero, which means there were no changes to save.
2. The call succeeds and returns a non-zero value, which means there were changes and they were saved.
3. The call fails and an exception is thrown.

Which is it in your case and, if it's #3, what is the error message?
 
Back
Top