Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Lists and DataGridView

Posted on 2014-01-13
6
Medium Priority
?
363 Views
Last Modified: 2014-01-13
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!
0
Comment
Question by:slightlyoff
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 39776836
Have you set up your columns to use those properties?
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39776842
No, I suppose that's the problem.  Is that done programmatically or on the design side?
Thanks for the quick reply!
0
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 39776938
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:slightlyoff
ID: 39777115
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?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39777203
The DataSource property requires an object based on a class that implements IList. List already implements IList, so there is no need to define a BindingSource for most situations. The following should suffice:

DataGridView1.DataSource = Inventory
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 39777561
Thank you!  I think the fact I chose a BindingSource as my datasource when I dragged in the DatagridView threw me off.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question