Solved

Bind DataGridView to ArrayList

Posted on 2008-10-17
9
4,532 Views
Last Modified: 2013-11-07
I am trying to do something simple here...

I have a class..

    Public Class queryAttribute
        Public ccDisplayName As String = ""
        Public ccQueryType As Integer
        Public ccQueryValue As String = ""
    End Class

Then there is an arraylist of this class

dim myList as ArrayList

I call myList.add for a number of items and I can verify they are there.

Then I try to set the datasource of the datagridview gvAttributes to be myList

so gvAttributes.Datasource = myList

When I initialize the app I do this..

With gvAttributes
                .AutoGenerateColumns = False
                .ColumnHeadersVisible = False
                .Columns.Add("Type", "Type")
                .Columns.Add("Value", "Value")
                Dim colBmp As New Bitmap(Application.StartupPath & "\resources\delete.gif")
                Dim colImg As Image
                colImg = colBmp

                Dim col As New DataGridViewImageColumn
                col.Image = colImg
                .Columns.Add(col)
            End With

Then when the content of the arraylist is changed I do this..

                With gvAttributes
                    '.AutoGenerateColumns = True
                    .ColumnHeadersVisible = True
                    .Columns(0).DataPropertyName = "ccDisplayName"
                    .Columns(1).DataPropertyName = "ccQueryValue"
                End With
                gvAttributes.DataSource = myList

Can someone please tell me why I dont have rows in my grid? I can see when I look at the properties of the grid in "watch" that the datasource has rows.. why are they not showing up here?

Please advise.
0
Comment
Question by:NTGuru705
  • 4
  • 4
9 Comments
 
LVL 1

Author Comment

by:NTGuru705
ID: 22745331
any thoughts to help a poor soul here?
0
 
LVL 13

Expert Comment

by:sm394
ID: 22745690
try
gvAttributes.Datasource = myList
gvAttributes.DataBind() ' if its missing
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22746454
sm394,
DataBind() method is only for ASP.NET gridviews not for DataGridView
 
NTGuru705,
You are trying to bind the datagridview with the collection but you cannot do that way. Create a new datatable (instead of the class) and add the new datarows. Then you can bind easily.
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 22746959
I am not sure I follow 100% could you elaborate on this please?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 22747387
Small example
        Dim dt As New DataTable

        Dim rnd As New Random()
 

        dt.Columns.Add(New DataColumn("ID", GetType(Integer)))

        dt.Columns.Add(New DataColumn("Description", GetType(String)))

        dt.Columns.Add(New DataColumn("Value", GetType(Integer)))

 

        ' Here you add the values

        For x As Byte = 1 To 250

            Dim dr As DataRow = dt.NewRow()

            dr("ID") = x

            dr.Item("Description") = "Description " & x.ToString

            dr.Item("Value") = rnd.Next

            dt.Rows.Add(dr)

        Next

 

       ' Bind the information to the DataGridView1 

       ' (you don't need to create the columns before)

        Me.DataGridView1.DataSource = dt

Open in new window

0
 
LVL 1

Author Comment

by:NTGuru705
ID: 22750023
I suppose I am confused in why the arraylist doesnt work.. I understand your workaround.. (thank you for example) but I would like to understand why the arraylist binds but doesnt display... could you help me understand that one?

Thank you for your help.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22750034
Because you're not binding a regular arraylist but a structure. Datagridview doesn't guess that!
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 22753206
so in what cases would binding an arraylist work? It seems the most effective way to use an array list is an array of a class.  What is a case that would work?  I am using an arraylist because it is being maintained by the UI and things are added/removed from the list.  So I just use the .remove to take the item out...

I suppose I could do just the same with a datatable but I am trying to understand the why behind it.. could you give me a scenario (other than a simple arraylist of strings) that would work?  Are there any scenarios where binding to a datagridview with an arraylist would work?

Thanks
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22813226
so in what cases would binding an arraylist work?
 
If you use a regular arraylist, just with values and not structures, it will work.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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