我该如何解决? [英] How can i fix that ?

查看:89
本文介绍了我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当图像存在于数据库中时,它将正确显示在pictureBox上.
如果图像不存在,则显示错误.

代码就在这里!

When the image exists at the database , It shows correctly at pictureBox.
If the image doesn''t exist, it shows error.

The Code is Here !

Private Sub gdCompany_CellClick_1(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gdCompany.CellClick
      btnSave.Text = "ျပင္ရန္"
      picCompanyLogo.Visible = True
      txtCompanyName.Text = gdCompany.CurrentRow.Cells(1).Value.ToString()
      txtDescription.Text = gdCompany.CurrentRow.Cells(2).Value.ToString()
      txtPhoneNo.Text = gdCompany.CurrentRow.Cells(3).Value.ToString()
      txtFaxNo.Text = gdCompany.CurrentRow.Cells(4).Value.ToString()
      txtEmail.Text = gdCompany.CurrentRow.Cells(5).Value.ToString()
      txtWebsite.Text = gdCompany.CurrentRow.Cells(6).Value.ToString()
      ddlBankName.SelectedValue = Convert.ToInt32(gdCompany.CurrentRow.Cells(7).Value.ToString())
      ddlBankBranch.SelectedValue = Convert.ToInt32(gdCompany.CurrentRow.Cells(8).Value.ToString())
      txtBankAccountNo.Text = gdCompany.CurrentRow.Cells(9).Value.ToString()
      txtAddress.Text = gdCompany.CurrentRow.Cells(10).Value.ToString()
      Try
          'Get image data from gridview column.
          Dim imageData As Byte() = DirectCast(gdCompany.CurrentRow.Cells(15).Value, Byte())
          'Initialize image variable
          Dim newImage As Image
          'Read image data into a memory stream
          Using ms As New MemoryStream(imageData, 0, imageData.Length)
              ms.Write(imageData, 0, imageData.Length)
              'Set image variable value using memory stream.
              newImage = Image.FromStream(ms, True)
          End Using
          'set picture
          picCompanyLogo.Image = newImage
      Catch ex As Exception
          MessageBox.Show(ex.ToString())
          txtCompanyName.Focus()
      End Try
  End Sub

推荐答案

注释MessageBox:
Comment the MessageBox :
...
        Catch ex As Exception
            'MessageBox.Show(ex.ToString()) 'commented the message box
            txtCompanyName.Focus()
        End Try


我建​​议检查gdCompany.CurrentRow.Cells(15).Value为null的可能性,然后尝试将其强制转换为字节数组并尝试填充pictureBox.

更新:

I suggest checking for the possibility of gdCompany.CurrentRow.Cells(15).Value being null before you try to cast it to a byte array and try to populate the pictureBox.

Update:

if(gdCompany.CurrentRow.Cells(15).Value != null)
{
//your code here..
}


这是我的工作,当数据库中不存在该图像时,我想在我的资源中显示一个名为"unknownUser"的示例图像.

here is what i do, when if the image is not present in database i want to show a sample image,named "unknownUser" in my resources.

Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.ColumnIndex = 1 Then
            Dim cell As DataGridViewImageCell = DataGridView1.Rows(e.RowIndex).Cells(1)
            cell.Value = My.Resources.UnknownUser
        End If
    End Sub



因此,如果使用此选项,则网格中将不会出现任何错误的"X"图像,并且您的代码也不会引发任何异常.



so if you use this, there wont be any error "X" image in your grid, and your code wont throw any exception at all.


这篇关于我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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