Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Lists and DataGridView

Posted on 2014-01-13
6
358 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 26

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 26

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 52
Expression Evaluater 3 37
vb.net class 3 21
FInd Image Control Gridview 3 21
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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