Results 1 to 5 of 5

Thread: [RESOLVED] [2005] displaying list(of type) in datagridview

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Resolved [RESOLVED] [2005] displaying list(of type) in datagridview

    i'm trying to display a list(of type) in a datagridview.
    the datagridview has 2 columns, the 1st is a text column + the 2nd is a checkbox column. the datagridview resizes to the correct size but it doesn't display anything. what am i doing wrong?

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Structure record
    4.         Dim lastDatetime As String
    5.         Dim checks As Boolean
    6.     End Structure
    7.     Private records As New List(Of record)
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         For x As Integer = -100 To -10 Step 10
    11.             Dim rec As New record
    12.             Dim dt As Date = Now.AddDays(x)
    13.             rec.lastDatetime = dt.ToString
    14.             rec.checks = True
    15.             records.Add(rec)
    16.         Next
    17.         Dim rec1 As New record
    18.         rec1.lastDatetime = Now.ToString
    19.         rec1.checks = False
    20.         records.Add(rec1)
    21.  
    22.         dgv1.DataSource = records
    23.         dgv1.Update()
    24.  
    25.     End Sub
    26.  
    27. End Class

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] displaying list(of type) in datagridview

    Can you definitely use Structures to populate a dataGridView?

    I have absolutely no idea as to the answer, just thought i'd ask. I just wonder if you are trying to bind a custom object - is the data in your strucure properly exposed if that makes sense?

    Have you done this before and it has worked? If so then i'll stop talking.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] displaying list(of type) in datagridview

    no i haven't done it before, and i didn't know if it was possible.
    i've tried a slightly different approach now, but it still doesn't work.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Public Structure record
    4.         Dim lastDatetime As String
    5.         Dim checks As Boolean
    6.     End Structure
    7.     Private records As New List(Of record)
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.  
    11.         For x As Integer = -100 To -10 Step 10
    12.             Dim rec As New record
    13.             Dim dt As Date = Now.AddDays(x)
    14.             rec.lastDatetime = dt.ToString
    15.             rec.checks = True
    16.             records.Add(rec)
    17.         Next
    18.         Dim rec1 As New record
    19.         rec1.lastDatetime = Now.ToString
    20.         rec1.checks = False
    21.         records.Add(rec1)
    22.  
    23.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of record)))
    24.         Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
    25.         serializer.Serialize(fs, records)
    26.         fs.Close()
    27.  
    28.         Dim ds As New DataSet
    29.         ds.ReadXml("test.xml")
    30.         ds.Tables(0).Columns(0).ColumnName = "last access time"
    31.         ds.Tables(0).Columns(1).ColumnName = "checks?"
    32.  
    33.         dgv1.AutoGenerateColumns = False
    34.         dgv1.DataSource = ds.Tables(0)
    35.         dgv1.Update()
    36.  
    37.     End Sub
    38.  
    39. End Class

    it resizes the datagridview to the right number of rows, but it still doesn't display anything

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] displaying list(of type) in datagridview

    i think you'll have to use a BindingList(Of T) instead of a List(Of T). I think thats the name of it...
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] displaying list(of type) in datagridview

    i've got it working with the dataset now. i did try a BindingList earlier.
    i forgot to set the column properties properly.
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width