在单个表单中显示多个记录

时间:2015-07-30 05:30:42

标签: vb.net

我有一个名为familydata的表,列[ID], [Name], [Photo] 我已经添加了5条记录。
现在我想显示表格中每条记录的所有照片 我已将5 picutreboxes添加到表单中,但我不知道如何仅显示所有照片。我不想使用DataGrid。 请告诉我如何将所有照片显示到我的特定pictureboxes

如果你给代码会更有帮助..我是vb.net的新手

我试过了,但它只显示了一张照片:

Dim cn As New SqlConnection()
            cn.ConnectionString = "Data Source=.;uid=sa;pwd=wintellect;database=master"
            cn.Open()
            Dim sql As String = "Select * from familydata where id=" + textbox1.text.trim()
            Dim cmd As New SqlCommand(sql, cn)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                        dr.Read()
                        Dim data As Byte() = DirectCast(dr("Photo"), Byte())
                        Dim ms As New MemoryStream(data)
                        pictureBox1.Image = Image.FromStream(ms)
            End If
            cn.Close()

ID不是主键。一个家庭有相同的ID 例如: 一个家庭1个,其成员相同 2为第二个家庭,其成员为相同

2 个答案:

答案 0 :(得分:0)

好吧,既然你所代表的数据是相同的,你可以选择一个UserControl,它包含你想要代表的数据的访问权。

所以你可以先创建一个代表Family实体的小类:

Public Class Family
    Public Property ID As Integer

    Public Property Photo As System.IO.Stream

    Public Property Name As String
End Class

要表示此数据,您可以像这样设置用户控件

Public Class FamilyDisplayControl
    Inherits UserControl

    Private tableControl As TableLayoutPanel
    Private familyPhoto As PictureBox
    Private familyName As Label

    Private _family As Family
    Public ReadOnly Property Family As Family
        Get
            Return _family
        End Get
    End Property

    Protected Sub InitializeComponent()
        Me.SuspendLayout()
        Me.Width = 128
        Me.Height = 96

        tableControl = New TableLayoutPanel()
        tableControl.Dock = DockStyle.Fill

        familyPhoto = New PictureBox()
        familyPhoto.SizeMode = PictureBoxSizeMode.AutoSize
        If Not (Family.Photo Is Nothing) Then
            familyPhoto.Image = Image.FromStream(Family.Photo)
        End If
        tableControl.Controls.Add(familyPhoto, 0, 0)

        familyName = New Label()
        familyName.AutoSize = True
        familyName.Text = Family.Name
        tableControl.Controls.Add(familyName, 0, 1)

        Me.Controls.Add(tableControl)
        Me.ResumeLayout()
    End Sub

    Public Sub New()
        Me._family = New Family() With {.ID = -1, .Name = String.Empty, .Photo = Nothing}
        InitializeComponent()
    End Sub

    Public Sub New(family As Family)
        Me._family = family
        InitializeComponent()
    End Sub
End Class

然后,当您读取数据并为每一行创建Family实体的新实例时,可以添加新控件,例如:FlowLayoutPanel

此流程布局面板将根据您的家庭数据显示数据,自动调整表单中的内容大小,并在数据库中包含对您家庭成员的引用

这样你就可以这样做

Dim cn As New SqlConnection()
cn.ConnectionString = "Data Source=.;uid=sa;pwd=wintellect;database=master"
cn.Open()
Dim sql As String = "select * from familydata"
Dim cmd As New SqlCommand(sql, cn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
    flowLayoutPanel1.Controls.Clear()
    While dr.Read()
        Dim data As Byte() = DirectCast(dr("Photo"), Byte())
        Dim ms As New MemoryStream(data)
        Dim familyEntity as new Family With { .ID = Convert.ToInt32(dr("ID")), .Name = dr("Name").ToString(), .Photo = ms }
        flowLayoutPanel1.Add(new FamilyDisplayControl(familyEntity))
    End While
End If
cn.Close()

它会为您提供flowlayoutpanel内显示的家庭表的完整内容,其中包含图片和家庭名称

答案 1 :(得分:0)

您可以使用DataRepeater控件执行此操作。

首先,

  1. DataRepeater控件添加到表单
  2. 打开Data Sources窗口和数据源。您可以使用类型化数据集。
  3. 将数据集(“数据源”窗口)中的字段拖到DataRepeater中。按照你想要的样子设计它。
  4. 您可以在此找到几个教程。

    以下是其中一些:

    1. https://youtu.be/Ox9w_LLyw38(视频教程)

    2. http://blogs.msdn.com/b/vsdata/archive/2009/08/12/datarepeater-control-for-windows-forms.aspx

    3. 一旦你开始了解它是如何工作的,你就可以开始自定义它或者从代码中进行数据绑定等。