|
-
Feb 12th, 2008, 04:06 PM
#1
[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:
Public Class Form1
Public Structure record
Dim lastDatetime As String
Dim checks As Boolean
End Structure
Private records As New List(Of record)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = -100 To -10 Step 10
Dim rec As New record
Dim dt As Date = Now.AddDays(x)
rec.lastDatetime = dt.ToString
rec.checks = True
records.Add(rec)
Next
Dim rec1 As New record
rec1.lastDatetime = Now.ToString
rec1.checks = False
records.Add(rec1)
dgv1.DataSource = records
dgv1.Update()
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 12th, 2008, 04:57 PM
#2
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.
-
Feb 12th, 2008, 05:10 PM
#3
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:
Public Class Form1
Public Structure record
Dim lastDatetime As String
Dim checks As Boolean
End Structure
Private records As New List(Of record)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = -100 To -10 Step 10
Dim rec As New record
Dim dt As Date = Now.AddDays(x)
rec.lastDatetime = dt.ToString
rec.checks = True
records.Add(rec)
Next
Dim rec1 As New record
rec1.lastDatetime = Now.ToString
rec1.checks = False
records.Add(rec1)
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of record)))
Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
serializer.Serialize(fs, records)
fs.Close()
Dim ds As New DataSet
ds.ReadXml("test.xml")
ds.Tables(0).Columns(0).ColumnName = "last access time"
ds.Tables(0).Columns(1).ColumnName = "checks?"
dgv1.AutoGenerateColumns = False
dgv1.DataSource = ds.Tables(0)
dgv1.Update()
End Sub
End Class
it resizes the datagridview to the right number of rows, but it still doesn't display anything
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 12th, 2008, 06:28 PM
#4
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...
-
Feb 12th, 2008, 06:39 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|