Adding a new line

When I try to add a new line I get the following error:  "Object reference not set to an instance of an object."  The code I am using is as follows:

 Private Sub gridPipe_AddNewRowInitiating(sender As Object, e As AddNewRowInitiatingEventArgs) Handles gridPipe.AddNewRowInitiating
        Try
            Dim data = TryCast(e.NewObject, Sales20._20SalesDataTable)
            data.FrameDollarColumn.Equals(0.0)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

What am I missing in my code?

4 Replies

AA Arulraj A Syncfusion Team September 26, 2018 05:42 AM UTC

Hi Derek, 

Thanks for contacting Syncfusion support. 

We suspect that the NullReferenceException occurs due to the casting of NewObject to Sales20._20SalesDataTable. While using DataTable as data source, you should cast the NewObject to DataRowView. Please refer to the following code example and sample from the given location. 

Code example : 
AddHandler Me.sfDataGrid.AddNewRowInitiating, AddressOf sfDataGrid_AddNewRowInitiating 
 
Private Sub sfDataGrid_AddNewRowInitiating(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs) 
       Dim data = TryCast(e.NewObject, DataRowView) 
       data.Row.SetField("ID", 1013) 
       data.Row.SetField("Name", "Oliver") 
End Sub 


Regards, 
Arulraj A 



RS Rod Schmitzer August 25, 2021 03:25 AM UTC

Hi,

I was looking for ways to create default values for a new row and I found this old post.

I downloaded the sample code you shared but I get an error.

It seems that  TryCast(e.NewObject, DataRowView) returns Nothing.

Is this code still valid or does it need to be done a different way now?


Regards,

Rod



RS Rod Schmitzer August 25, 2021 04:33 AM UTC

Further to my previous email, it seems that casting NewObject as a datarow instead of a datarowview works.

The following code works for me.

        Dim NewRow As System.Data.DataRow = TryCast(e.NewObject, System.Data.DataRow)

        NewRow.Item("MachineNo") = 1

        NewRow.Item("PortNo") = 4370

        NewRow.Item("ComNo") = 0




VS Vijayarasan Sivanandham Syncfusion Team August 25, 2021 05:12 AM UTC

Hi Rod Schmitzer,

Thank you for contacting Syncfusion Support.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon