In Feburary 2008 the VB Power packs team released version 3 of there VB Power Packs 2005. This version included a DataRepeater control.
The DataRepeater control allows you use standard windows controls to display your data in a scrollable container. The included documentation shows you how to bind the DataRepeater to a typed dataset. This article will show you how to bind to a datatable in code.
To start off with create a new windows forms application and drop a datarepeater on the form. In the datarepeater drop a picturebox (pbCategory) and a label (lblName).
In the forms load event lets add some code to connect to the database. I am binding the datarepeater to a bindingsource because it is a good practice.
Dim bs As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection("Server = .\SQLExpress;Database = NorthWind; Integrated Security = SSPI;")
Dim dt As New DataTable
Dim da As New SqlDataAdapter("Select * from Categories", conn)
da.Fill(dt)
bs.DataSource = dt
DataRepeater1.DataSource = bs
End Sub
The DataRepeater has a DrawItem event which allows us to put the data in the controls for each item. You can also use this event for formatting the data for display
Private Function GetBitmap(ByVal Pic() As Byte) As Bitmap
Dim ms As New System.IO.MemoryStream
Dim bm As Bitmap
ms.Write(Pic, 78, Pic.Length - 78)
bm = New Bitmap(ms)
Return bm
End Function
Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem
Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex)
DirectCast(e.DataRepeaterItem.Controls("pbCategory"), PictureBox).Image = GetBitmap(DirectCast(currItem.Item("Picture"), Byte()))
DirectCast(e.DataRepeaterItem.Controls("lblName"), Label).Text = currItem.Item("CategoryName").ToString
End Sub