Solved

Lists and DataGridView

Posted on 2014-01-13
6
350 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now