Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Lists and DataGridView

Posted on 2014-01-13
6
Medium Priority
?
362 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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