Question BindingSource.AddNew() does not fire dataset.datatable.tablenewRow

king_jeremy

Member
Joined
Oct 20, 2009
Messages
13
Programming Experience
Beginner
Hi,
I have a problem with handling tablenewrow event. When I call bindingSource.addNew() method to add a new row to its bound dataset.datatable which is empty in the beginning the tablenewrow event is not fired. I kinda get the logic behind this probably the datatable is not initialized until it has some data in it but how am I supposed to call tablenewrow event when I call bindingSource.addNew() method with an empty dataset.datatable?
 
I think you'll find that you're mistaken. Try creating a new WinForms project and add two Buttons and a BindingSource to the form, then add this code:
VB.NET:
Private WithEvents table As New DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table.Rows
        .Add(1, "Peter")
        .Add(2, "Paul")
        .Add(3, "Mary")
    End With

    Me.BindingSource1.DataSource = table
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim row = Me.table.NewRow
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim row = Me.BindingSource1.AddNew
End Sub

Private Sub table_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles table.TableNewRow
    MessageBox.Show("New Row")
End Sub
Try running that and clicking the Buttons. As you can see, TableNewRow is raised whether you create the new row via the DataTable or via the BindingSource.

I'm guessing that you're handling the event incorrectly, but we'd have to see what you're doing to know what you're doing wrong.
 
What exactly I do is this.
I use a typed dataset in the project, I want to create an "add new record form" so I select the table from the datasource window and select details drag it onto the form to create the controls since it is a typed dataset it is not filled with anything initially. I call bindingSource.addnew() to create an empty row and since I want to set some default values to some fields I also handle the tablenewrow event. Here is the code

VB.NET:
    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PatientsBindingSource.AddNew()
        AddHandler OperationNotebookDataSet.patients.TableNewRow, AddressOf PatientsTableNewRow
    End Sub
    Sub PatientsTableNewRow(ByVal sender As Object, ByVal e As DataTableNewRowEventArgs)
        Dim r As DataEntities.operationNotebookDataSet.patientsRow
        r = e.Row()
        r.creationDate = Now()
    End Sub
This way it doesn't work.
 
Last edited:
You're calling AddNew before you're adding the event handler. If you want that method to be invoked when you call AddNew then you need to add it as an event handler BEFORE you call AddNew, Not AFTER.
 
DataEntities.operationNotebookDataSet.patients.creationDateColumn.DefaultValue = DateTime.Now
PatientsBindingSource.AddNew()
 
Back
Top