Error Bindingsource

I use a Form with a Datagridview-Control (DataGridConn), a Datatable (Testtab) and a Bindingsource (Conn). When I add a new row in the Grid and want to set some starting values (XXXX) in the Conn_AddingNew event I get the error:

Objects added to a BindingSource's list must all be of the same type.


The used code is the following:



Public
Class frmFormConn

Dim WithEvents Conn As BindingSource

Dim TestTab As DataTable

Private Sub frmFormConn_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim i As Integer

Conn = New BindingSource

TestTab = New DataTable

TestTab.Columns.Add(New DataColumn("NAME", GetType(String)))

Conn.DataSource = TestTab

DataGridConn.DataSource = Conn

For i = 0 To 10

TestTab.Rows.Add(TestTab.NewRow)

TestTab.Rows(i)("NAME") = "NAME" & CStr(i)

Next

End Sub

Private Sub Conn_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles Conn.AddingNew

Dim dr As DataRow = TestTab.NewRow

dr("NAME") = "XXXXX"

e.NewObject = dr

End Sub

end Class


I would be very grateful to get any help for this problem


Best regards
Klaus Unterforsthuber



Answer this question

Error Bindingsource

  • Masroor ul Hassan

    hi,

    i think you don't have to assign the default value to your binding source you can achieve it during creation of your table like this

    Public Class Form1

    Dim Conn As BindingSource

    Dim TestTab As DataTable

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim i As Integer

    Conn = New BindingSource

    TestTab = New DataTable

    Dim NameCol As New DataColumn

    With NameCol

    .DataType = GetType(String)

    'here you can assign a value to the new row

    .DefaultValue = "xxxxx"

    .ColumnName = "name"

    End With

    TestTab.Columns.Add(NameCol)

     

    Conn.DataSource = TestTab

    DataGridconn.DataSource = Conn

    For i = 0 To 10

    TestTab.Rows.Add(TestTab.NewRow)

    TestTab.Rows(i)("NAME") = "NAME" & CStr(i)

    Next

     

    End Sub

    End Class

    hope it helpls



  • Raymond Roelands

    I found out that it has to be a DataRowView in this particular case.

    Regards, HalH


  • BreSmith

    Klaus,

    I have the same problem.

    Did you solve it

    Regards, HalH.

     


  • Error Bindingsource