You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2010中点击DataGridView如何在PictureBox加载对应单张或多张图片

实现步骤

1. 窗体控件准备

  • 添加PictureBox控件,命名为picShow,将SizeMode属性设置为Zoom,保证图片自适应显示
  • 可选:添加2个Button控件,分别命名为btnPrev、btnNext,文本设置为「上一张」「下一张」,用于多张图片切换

2. 完整代码修改

Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Dim Path As String = "C:\Users\Administrator\Desktop\DBF"
    Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
    Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    ' 存储当前选中行的有效图片路径
    Dim currentImageList As New List(Of String)
    ' 当前显示的图片索引
    Dim currentImageIndex As Integer = 0

    Private Sub PopulateDataGridView()
        Try
            Dim dt = New DataTable()
            Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"
            Using adapter As New OleDbDataAdapter(query, cn.ToString)
                adapter.Fill(dt)
            End Using
            Me.DataGridView1.DataSource = dt
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PopulateDataGridView()
    End Sub

    ' DataGridView选中行变化事件
    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        ' 清空旧数据、释放旧图片资源
        currentImageList.Clear()
        currentImageIndex = 0
        If picShow.Image IsNot Nothing Then
            picShow.Image.Dispose()
            picShow.Image = Nothing
        End If
        ' 无选中行直接返回
        If DataGridView1.SelectedRows.Count = 0 Then Return
        Dim selectedRow = DataGridView1.SelectedRows(0)
        ' 收集4个字段的有效图片路径
        For i = 1 To 4
            Dim fileNameVal = selectedRow.Cells($"FILENAME{i}").Value
            If fileNameVal IsNot DBNull.Value AndAlso Not String.IsNullOrWhiteSpace(fileNameVal.ToString()) Then
                Dim fullImgPath = Path.Combine(Pathimage, fileNameVal.ToString().Trim())
                If File.Exists(fullImgPath) Then
                    currentImageList.Add(fullImgPath)
                End If
            End If
        Next
        ' 存在有效图片则显示第一张
        If currentImageList.Count > 0 Then
            LoadCurrentImage()
        End If
    End Sub

    ' 加载当前索引对应图片
    Private Sub LoadCurrentImage()
        If picShow.Image IsNot Nothing Then
            picShow.Image.Dispose()
        End If
        picShow.Image = Drawing.Image.FromFile(currentImageList(currentImageIndex))
        ' 可自行添加标签显示图片数量:lblTip.Text = $"第{currentImageIndex + 1}张 / 共{currentImageList.Count}张"
    End Sub

    ' 上一张按钮点击事件
    Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
        If currentImageList.Count = 0 OrElse currentImageIndex = 0 Then Return
        currentImageIndex -= 1
        LoadCurrentImage()
    End Sub

    ' 下一张按钮点击事件
    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        If currentImageList.Count = 0 OrElse currentImageIndex = currentImageList.Count - 1 Then Return
        currentImageIndex += 1
        LoadCurrentImage()
    End Sub
End Class

说明

如果不需要切换展示,想要同时显示所有图片,只需在窗体上添加4个PictureBox控件,在SelectionChanged事件中直接给4个控件分别赋值对应路径的图片即可,无需切换逻辑和索引变量。

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 05:45:03