Stanislaw Kuncik Stanislaw Kuncik - 3 months ago 17
Vb.net Question

Add data into new added row in `DataGridView` from `DataBindingSource`

In

Form4
i have a
DataGridView
named
DbTableDataGridView
.
In
Form3
there is a set of fields (text boxes) that are all bound to the
DbTableBindingSource
. When I run application the
Form4
shows up. There is a button to open new form (
Form3
) and in there enter details about customers to be added as new row into database (
DataGridView
). My code for the "Add" button in
Form4
looks like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Me.DbTableDataGridView.Refresh()
Me.DbTableBindingSource.AddNew()

Form3.ShowDialog()

Form3.ImiéTextBox.Text = ""
Form3.NazwiskoTextBox.Text = ""
Form3.Numer_TelefonuTextBox.Text = ""
Form3.Numer_RejestracyjnyTextBox.Text = ""
Form3.MarkaTextBox.Text = ""
Form3.ModelTextBox.Text = ""
Form3.Poj_SilnikaTextBox.Text = ""
Form3.RocznikTextBox.Text = ""
Form3.PaliwoTextBox.Text = ""
Form3.Data_PrzyjeciaDateTimePicker.Value = DateTime.Now
Form3.RichTextBox1.Text = ""

End Sub


It does add new row, selects it and clears entries in the text boxes (that are bound into 'DbTableBindingSource'.
In this form after I fill in all the fields I press button save:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Try



Me.Validate()

Form4.DbTableBindingSource.EndEdit()
Me.DbTableTableAdapter.Update(CartronicDBDataSet.dbTable)
TableAdapterManager.UpdateAll(CartronicDBDataSet)
DbTableTableAdapter.Fill(Form4.CartronicDBDataSet.dbTable)

MsgBox("Saved")


Catch ex As Exception
MessageBox.Show("Blad zapisu. Sprobuj ponownie. W razie potrzeby zamknij, a nastepnie uruchom ponownie program Cartronic")
End Try


End Sub


It goes to the message "Saved" but actually does not fill in added new recently.
Any thoughts?

Answer

I have done what you have suggested but little bit simpler. Assigned all text boxes to each cell in current row as follows:

Form4.DbTableDataGridView.CurrentRow.Cells(5).Value = Me.NazwiskoTextBox.Text.ToString
        Form4.DbTableDataGridView.CurrentRow.Cells(4).Value = Me.ImiéTextBox.Text.ToString

It works fine. Cheers

Comments