none
Binding to Winforms in VB RRS feed

  • Question

  • Hello,

    I have started migrating a Windows Forms + Linq to SQL application to Entity Framework.

    Finally, the code now compiles, but I run into many problems related to binding data to BindingSources in Windows Forms.

    Most of the forms are populated with code similar to this :

    Dim myData = From e In DataContext.myEntitys _

    Where e.Code = 1 _ Order By e.Name _ Select e myBindingSource.DataSource = myData

    To add a row, the typical piece of code is :

    Dim e as New myEntity
    myBindingSource.Add(e)

    However, nothing seems to work.... Loading dataBingSource.Datasource crashes. Adding a row doesn't work.

    I have found this interesting article http://msdn.microsoft.com/en-us/data/jj682076, but it is written in C# and doesn't compile in VB :

    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Imports System.Diagnostics.CodeAnalysis
    Imports System.Data.Entity
    
    Public Class ObservableListSource(Of T)
        Inherits ObservableCollection(Of T)
        Implements IListSource
    
        Private _bindingList As IBindingList
        ReadOnly Property IListSource_ContainsListCollection As Boolean Implements IListSource.ContainsListCollection
            Get
                Return False
            End Get
        End Property
        Function IListSource_GetList() As IList Implements IListSource.GetList
            If _bindingList Is Nothing Then
                _bindingList = Me.ToBindingList()
            End If
            Return _bindingList
        End Function
    End Class
    

    So what should I do to bind data to Winforms Databindingsource ?

    Thanks

    Thursday, May 29, 2014 3:17 PM

Answers

  • Hello Sygrien,

    >>But I still cannot find the "DbSet.Load" method and the "DbSet.Local.ToBindingList" method.

    They are extend methods under System.Data.Entity namespace, and in VB, as far as I know, it wouldn’t add it automatically, so we need to import it by hand as:

    Imports System.Data.Entity
    
    Public Class Form1
    
    
        Dim DB As DFDBEntities
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            DB = New DFDBEntities()
    
    
            DB.Orders.Load()
    
    
            DataGridView1.DataSource = DB.Orders.Local.ToBindingList()
    
    
        End Sub
    
    End Class

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred BaoModerator Friday, May 30, 2014 1:56 AM
    • Proposed as answer by ErikEJMVP Friday, May 30, 2014 8:10 AM
    • Marked as answer by Sygrien Friday, May 30, 2014 11:10 AM
    Friday, May 30, 2014 1:55 AM
    Moderator

All replies

  • Ok, I have made progress....

    The ObservableListSource class now compiles in VB. There was a small error in my code. The class declaration should be :

    Public Class ObservableListSource(Of T As Class)

    But I still cannot find the "DbSet.Load" method and the "DbSet.Local.ToBindingList" method.

    Thursday, May 29, 2014 4:09 PM
  • Hello Sygrien,

    >>But I still cannot find the "DbSet.Load" method and the "DbSet.Local.ToBindingList" method.

    They are extend methods under System.Data.Entity namespace, and in VB, as far as I know, it wouldn’t add it automatically, so we need to import it by hand as:

    Imports System.Data.Entity
    
    Public Class Form1
    
    
        Dim DB As DFDBEntities
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            DB = New DFDBEntities()
    
    
            DB.Orders.Load()
    
    
            DataGridView1.DataSource = DB.Orders.Local.ToBindingList()
    
    
        End Sub
    
    End Class

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred BaoModerator Friday, May 30, 2014 1:56 AM
    • Proposed as answer by ErikEJMVP Friday, May 30, 2014 8:10 AM
    • Marked as answer by Sygrien Friday, May 30, 2014 11:10 AM
    Friday, May 30, 2014 1:55 AM
    Moderator