Link to home
Start Free TrialLog in
Avatar of NTGuru705
NTGuru705Flag for United States of America

asked on

Bind DataGridView to ArrayList

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.
Avatar of NTGuru705
NTGuru705
Flag of United States of America image

ASKER

any thoughts to help a poor soul here?
try
gvAttributes.Datasource = myList
gvAttributes.DataBind() ' if its missing
Avatar of Jorge Paulino
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.
I am not sure I follow 100% could you elaborate on this please?
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Because you're not binding a regular arraylist but a structure. Datagridview doesn't guess that!
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
so in what cases would binding an arraylist work?
 
If you use a regular arraylist, just with values and not structures, it will work.