DataRepeater-显示图像 [英] DataRepeater - Display Image

查看:83
本文介绍了DataRepeater-显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我在互联网上看到了以下代码:

Hello

I saw this code in the internet:

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


DataRepeater有一个DrawItem事件,该事件使我们可以将数据放入每个项目的控件中.您还可以使用此事件来格式化显示数据


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


我的问题是我在"


My problem is I encountered an error "Object reference not set to an instance of an object." on the line "

DirectCast(e.DataRepeaterItem.Controls("pbCategory"), PictureBox).Image = GetBitmap(DirectCast(currItem.Item("Picture"), Byte()))"


你们能告诉我是什么导致我出现此错误吗?在此先感谢:)


Can you guys tell me whats causing me this error? Thanks in advance :)

推荐答案

对象引用未设置为对象的实例

当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [e.DataRepeaterItem.Controls("pbCategory")Nothing.
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

For now, it seems like:
currItem.Item("Picture") or e.DataRepeaterItem.Controls("pbCategory") is Nothing.


这篇关于DataRepeater-显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆