Avatar of slightlyoff
slightlyoff
 asked on

Lists and DataGridView

I'm trying to use a DataGridView to display the contents of a list.

When I run the program the DataGridView is blank - so I'm doing something wrong.
My understanding (or misunderstanding) was that I could bind a list to a datagridview and that would be all there was to it.  For example:

Dim Inventory As New Collections.Generic.List(Of itemstochange)
'Loaded Inventory in the next few lines, left out to keep the question shorter
'I've looped through Inventory and it does fill correctly
'Inventory has three properties, sku, description & price

Dim source As New BindingSource
source.DataSource = Inventory

DataGridView1.DataSource = source

Open in new window


Do I have to loop through each item in the list to add it to the DataGridView?  I thought (hoped) that "binding" would pretty much take care of all of that for me.

I'm new to Lists and DataGridViews,  so please excuse this question if it's really simple.  :)

Thanks for your help!
Visual Basic.NET

Avatar of undefined
Last Comment
slightlyoff

8/22/2022 - Mon
Shaun Kline

Have you set up your columns to use those properties?
slightlyoff

ASKER
No, I suppose that's the problem.  Is that done programmatically or on the design side?
Thanks for the quick reply!
Shaun Kline

That may not necessarily be the problem. It just happened to be a DUH moment for me a few days back. You should consider using a BindingList instead of a BindingSource.

This blog provides an example of both: http://blogs.msdn.com/b/dchandnani/archive/2005/03/12/394438.aspx?Redirected=true
Your help has saved me hundreds of hours of internet surfing.
fblack61
slightlyoff

ASKER
Thanks again for your quick reply.  I tried following the examples in the link you sent, although I'm using VB.NET not C#.  

Here's the majority of my code.  It's still blank when I run it:

       'Originally used Generic List
       'Dim Inventory As New Collections.Generic.List(Of itemstochange)
       
       'using BindingList instead of Generic List...
       Dim Inventory As New BindingList(Of itemstochange)

        'To add something to your list will be
        Dim t As Integer
        Dim theSku As String = ""
        Dim theDesc As String = ""
        Dim thePrice As Double = 0
        Dim f1 As Integer = 0
        ProgressBar1.Maximum = lastSku
        ProgressBar1.Value = x
        For t = x To lastSku

        'Grab Items from excel worksheet, put into BindingList    
            If Len(xlWorkSheet.Cells(t, y).value) > 0 Then
                theSku = xlWorkSheet.Cells(t, y).Value.ToString
            Else
                f1 = 1
            End If

            If Len(xlWorkSheet.Cells(t, (y + 1)).value) > 0 Then
                theDesc = xlWorkSheet.Cells(t, (y + 1)).Value.ToString
            Else
                f1 = 1
            End If

            If Len(xlWorkSheet.Cells(t, (y + 6)).value) > 0 Then
                thePrice = CDbl(xlWorkSheet.Cells(t, (y + 6)).Value.ToString)
            Else
                f1 = 1
            End If

            If f1 <> 1 Then
                'Add to Binding List
                Inventory.Add(New itemstochange(theSku, theDesc, thePrice))
            Else
                f1 = 0
            End If
            ProgressBar1.Value = t
            Application.DoEvents()
        Next

        'Connect BindingSource to Binding list
        BindingSource1.DataSource = Inventory

        'Connect DataGridView to Binding Source
        DataGridView1.DataSource = BindingSource1

Open in new window


I set up BindingSource1 and DataGridView in the Form View (dragging them from the toolbox).  DataGridView1's datasource shows up as BindingSource1 in the Properties panel, so I might not need to do that last step - but I've tried it with and without and no luck.

Any ideas?
ASKER CERTIFIED SOLUTION
Jacques Bourgeois (James Burger)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
slightlyoff

ASKER
Thank you!  I think the fact I chose a BindingSource as my datasource when I dragged in the DatagridView threw me off.