Binding a List of Objects to a GridView in VB.NET

Setting a gridview to a list of objects in this instance we define a class TestResult to hold a list of TestResult, This class will hold a list of results for tests carried out on a  specific date.

We want to bind this to a gridview called  GridView_TestResults1 dynamically.

To do this in your web form add the GridView_TestResults1 and set AutoGenerateColumns to True.

Create a class TestResult add add this code

Public Class TestResult
      Private test_id As String
      Private test_date As DateTime
      Private test_result As String

      Public Property TestID() As String
                  Return test_id
            End Get
            Set(ByVal value As String)
                  test_id = value
            End Set
      End Property

      Public Property TestDate() As DateTime
                  Return test_date
            End Get
            Set(ByVal value As DateTime)
                  test_date = value
            End Set
      End Property

      Public Property TestResult() As String
                  Return test_result
            End Get
            Set(ByVal value As String)
                  test_result = value
            End Set
      End Property

      Public Sub New(ByVal t_id As String, ByVal t_date As DateTime, ByVal t_result As String)
            Me.test_id = t_id
            Me.test_date = t_date
            Me.test_result = t_result
      End Sub
End Class

Place this code either on your page load or in some other event handler e.g. on_click

            Dim tests As System.Collections.Generic.List(Of TestResult) = New System.Collections.Generic.List(Of TestResult)()

            Dim test_date As new DateTime(2011, 09, 01, 10, 15, 00);

            tests.Add(New TestResult("001", test_date, "Passed"))
            tests.Add(New TestResult("002", test_date, "Failed"))
            tests.Add(New TestResult("003", test_date, "Passed"))

            GridView_TestResults1.DataSource = tests


Popular posts from this blog

ActiveMQ, easy to use open source message oriented middleware (MOM)

Basic Send Message to MQ with Java and IBM MQ JMS

Apache Apollo