locked
Binding a collection to a Datagrid RRS feed

  • Question

  • I have been trying to bind a collection to a datagrid, and I am not getting anything. I was wondering whether someone can help.
    I know that the collection is being loaded correctly. But I have no idea how to get the datagrid to use the collection, apply headers, etc.

    Imports System.Collections.ObjectModel
    Imports System.Data
    
    Namespace x.Bll
    
        Public Class Profile
    
            Private _iProfileID As Integer
            Private _vProfileName As String
            Private _bProfileActive As Boolean
    
    
    
    #Region " Properties "
            Friend ReadOnly Property iProfileID() As Integer
                Get
                    Return _iProfileID
                End Get
            End Property
    
            Friend Property vProfileName() As String
                Get
                    Return _vProfileName
                End Get
                Set(ByVal value As String)
                    _vProfileName = value
                End Set
            End Property
    
            Friend Property bProfileActive() As Boolean
                Get
                    Return _bProfileActive
                End Get
                Set(ByVal value As Boolean)
                    _bProfileActive = value
                End Set
            End Property
    #End Region
    
    
    #Region " Constructors "
            Public Sub New()
                _iProfileID = -1
            End Sub
    
            Public Sub New(ByVal myRow As DataRow)
                LoadProfile(myRow)
            End Sub
    
            Private Sub LoadProfile(ByVal myRow As DataRow)
                _iProfileID = DirectCast(myRow("iProfileID"), Integer)
                vProfileName = CStr(myRow("vProfileName"))
                bProfileActive = CBool(myRow("bProfileActive"))
            End Sub
    #End Region
    
    
            Protected Friend Shared Function GetAllProfiles() As Collection(Of Profile)
                Dim r As New Collection(Of Profile)
    
                Dim P As New AutoReplyDSTableAdapters.ProfilesTableAdapter
                For Each i In P.GetData
                    r.Add(New Profile(i))
                Next
    
                Return r
            End Function
    
        End Class
    
    End Namespace


    And here is the form code I am trying to load the collection into:

    Public Class ProfilesList
    
        Public Sub New()
    
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
    
            Me.SuspendLayout()
            ' Add any initialization after the InitializeComponent() call.
            dgProfileList.DataSource = x.Bll.Profile.GetAllProfiles
    
            Me.ResumeLayout()
    
        End Sub
    End Class


    Saturday, October 17, 2009 6:41 PM

Answers

All replies