Click here to Skip to main content
Click here to Skip to main content

Binding a generic collection to a DataGridView via Visual Basic 2008 code

By , 16 Mar 2008
 

Introduction

I needed to bind a DataGridView to a generic collection of custom classes, in an application written in Visual Basic 2008. The objective to reach was that, whenever editing, adding, or deleting items from the DataGridView, the collection gets updated and vice versa.

Using the Code

The .NET Framework exposes a very useful object called BindingSource. This can be used also at design-time, but in this particular case, you’re not allowed to bind objects which are not defined as classes. In my own situation, I had to instantiate a collection inside a module, so this is a scenario which is not reachable at design-time using a BindingSource, but it is by writing a few lines of code. Let’s suppose we have the typical example class for learning, which is the Person class:

Public Class Person
    Private _lastName As String
    Private _name As String

    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Sub New()

    End Sub

    Public Sub New(ByVal name As String, ByVal lastName As String)
        _name = name
        _lastName = lastName
    End Sub
End Class

Now, let’s suppose we need to instantiate a collection of Person objects. In my own situation, this happens inside a Module, but in the example, it can happen inside the Sub Main of a Console application. This is the code:

Dim People As New List(Of Person)

The next step is to instantiate a BindingSource object and then assign its DataSource property to the above collection:

Dim PeopleBindingSource As New BindingSource
PeopleBindingSource.DataSource = People

In the Form containing the DataGridView, assign its DataSource property like this:

PeopleDataGridView.DataSource = PeopleBindingSource

Points of Interest

This kind of data-binding allows to simultaneously update the DataGridView and the bound collection, whichever of the two objects you’re going to edit. Moreover, in this particular case, if the AutoGenerateColumns property of the DataGridView control is set to True, the .NET Framework automatically generates as many columns as there are properties in the bound class (in our case, two columns, Name and LastName). Obviously, if you write code like the one shown above, you don’t need to set any data-binding property at design-time. I hope this article can be useful since on the Internet I found only articles about collections data-binding to a DataGridView where data is not editable, or the solution is only available in C#.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alessandro Del Sole
Other
Italy Italy
I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic, Visual Studio LightSwitch, and the .NET technologies.
 
Check out my blog at: http://community.visual-basic.it/AlessandroEnglish

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionFinally I understand!memberkeitht16-Nov-12 9:11 
Thanks for this post!
 
... however I cannot get it to work.
 
It runs without a problem, but the DataGridView Control is just a gray box. I cannot get it to show the data.
 
Any ideas what I am missing?
I am in VS2010 - VB.
 
Thanks,
Keith
GeneralMy vote of 5memberMitchell McKee14-May-11 14:56 
Clear and to the point. Thank you!
Questionhow to modify grid at runtimemember73amit23-Apr-08 23:50 
hi,
I need to modify the same grid at run time. My object ais updated at runtime i same updation is to be bind in datagrid.
Plz suggest.
Amit
AnswerRe: how to modify grid at runtimememberAlessandro Del Sole24-Apr-08 3:32 
The grid should be automatically updated but.. did you try to reassign the DataSource property?
 
Alessandro Del Sole
My Blog: http://community.visual-basic.it/Alessandro

GeneralRe: how to modify grid at runtimememberNickolais13-Jun-08 8:12 
Even better, set the DataSource property to a IBindingList or BindingList(Of T) and you dont have to reset the DataSource property. The changes will be reflected everytime your DataSource object is changed.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 16 Mar 2008
Article Copyright 2008 by Alessandro Del Sole
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid