【问题标题】:Image.FromStream is not a member of System.Windows.Forms.DataGridViewImageColumnImage.FromStream 不是 System.Windows.Forms.DataGridViewImageColumn 的成员
【发布时间】:2017-08-21 21:50:58
【问题描述】:

所以我使用此代码在DataGridView 中显示我的数据:

Sub display_Infodata()
    DGUSERS.Rows.Clear()
    Dim sql As New MySqlDataAdapter("select * from tbl_info", con)
    Dim ds As New DataSet
    DGUSERS.AllowUserToAddRows = False
    DGUSERS.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DGUSERS.RowTemplate.Height = 40
    sql.Fill(ds, 0)
    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
        Dim xx As Integer = DGUSERS.Rows.Add
        Dim uid As String = ds.Tables(0).Rows(i).Item(0).ToString
        Dim sqls As New MySqlDataAdapter("select * from tbl_other where userid='" & uid & "'", con)
        Dim dss As New DataSet

        sqls.Fill(dss, 0)
        With DGUSERS.Rows(xx)
            If dss.Tables(0).Rows.Count > 0 Then
                .Cells(0).Value = uid
                .Cells(1).Value = ds.Tables(0).Rows(i).Item(2).ToString
                .Cells(2).Value = ds.Tables(0).Rows(i).Item(3).ToString
                .Cells(3).Value = ds.Tables(0).Rows(i).Item(4).ToString
                .Cells(4).Value = ds.Tables(0).Rows(i).Item(5).ToString
                .Cells(5).Value = ds.Tables(0).Rows(i).Item(6).ToString
                .Cells(6).Value = dss.Tables(0).Rows(0).Item(1).ToString
                .Cells(7).Value = ds.Tables(0).Rows(0).Item(2).ToString
                .Cells(8).Value = ds.Tables(0).Rows(0).Item(8).ToString
                .Cells(9).Value = ds.Tables(0).Rows(0).Item("Image")
                .Cells(10).Value = dss.Tables(0).Rows(0).Item(2).ToString
                .Cells(11).Value = dss.Tables(0).Rows(0).Item(3).ToString
            Else

            End If
        End With
    Next
End Sub

它显示和工作,但我的问题是,当我尝试在另一个表单的DataGridView 中显示数据时,它显示以下错误:

这是我用的:

Try
    With View_Info
        Dim index As Integer
        Dim selectedRow As DataGridViewRow
        selectedRow = DGUSERS.Rows(index)

        .UserID.Text = DGUSERS.SelectedRows(0).Cells("UserID").Value
        .UserType.Text = DGUSERS.SelectedRows(0).Cells("UserType").Value
        .Fname.Text = DGUSERS.SelectedRows(0).Cells("Firstname").Value
        .Mname.Text = DGUSERS.SelectedRows(0).Cells("Middlename").Value
        .Lname.Text = DGUSERS.SelectedRows(0).Cells("Lastname").Value
        .Contact.Text = DGUSERS.SelectedRows(0).Cells("Contact").Value
        .Standing.Text = DGUSERS.SelectedRows(0).Cells("Standing").Value
        .Guardian.Text = DGUSERS.SelectedRows(0).Cells("Guardian").Value
        .ContactG.Text = DGUSERS.SelectedRows(0).Cells("GuardianContact").Value
        .DPCreated.Text = DGUSERS.SelectedRows(0).Cells("DateCreated").Value
        .DPValidity.Text = DGUSERS.SelectedRows(0).Cells("Validity").Value

        Dim img As Byte()
        img = DGUSERS.SelectedRows(0).Cells("Image").Value
        Dim ms As New MemoryStream(img)

        .UploadImage.Image = Image.FromStream(ms)
        .Show()
        .Focus()

    End With
Catch ex As Exception
    MsgBox(ex.Message & " Please select a corresponding records.", MsgBoxStyle.Exclamation)
End Try

有什么帮助吗?

【问题讨论】:

  • 我的问题是它说:'fromstream' 不是 system.windows.datagridviewimagecolumn 的成员
  • 欢迎来到 Stack Overflow!提示:格式化代码是通过缩进四个空格来完成的。反引号 ( ` ) 仅用于内联转义,而不是整个代码块。例如,当您谈论 Image.FromStreamDataGridView 等代码对象时会使用它们。
  • 查看markdown help了解更多信息。

标签: vb.net winforms datagridview xampp


【解决方案1】:

很难看到全貌,但问题很可能是您创建了一个DataGridViewImageColumn,您选择将其称为Image

编译器将始终选择局部变量、属性或类而不是同名的库/预导入命名空间对象。因此,将使用名称为 Image 而不是 System.Drawing.Image 的列,因为前者“更本地化”。

尝试指定命名空间,它应该可以工作:

.UploadImage.Image = System.Drawing.Image.FromStream(ms)

【讨论】:

  • 感谢您的帮助,我尝试使用 Dim pCell As New DataGridViewImageCell pCell = Me.DGUSERS.Item("Picture", e.RowIndex) .UploadImage.Image = byteArrayToImage(pCell.Value) 和它有效:) 我现在的问题是日期时间
  • @KirsthyLizo:DateTime 有什么问题,在哪一行?
  • 我收到此错误:该字符串未被识别为有效的日期时间。从索引 0 开始有一个未知单词。你看,我正在调用另一个 winform 来显示来自 datagridview 的数据
  • @KirsthyLizo :您最好提出一个新问题,并显示它发生的确切行以及您尝试转换的字符串。抛出异常时,使用调试器的Autos/Locals windows检查变量。
  • @KirsthyLizo:没问题,很高兴我能帮上忙!
【解决方案2】:

所以我解决这个问题的方法是:

Dim pCell As New DataGridViewImageCell
pCell = Me.DGUSERS.Item("Picture", e.RowIndex)

.UploadImage.Image = byteArrayToImage(pCell.Value)

并使用此功能:

Private Function byteArrayToImage(ByVal byt As Byte()) As Image

    Dim ms As New System.IO.MemoryStream()
    Dim drwimg As Image = Nothing

    Try
        ms.Write(byt, 0, byt.Length)
        drwimg = New Bitmap(ms)
    Finally
        ms.Close()
    End Try

    Return drwimg

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2014-10-30
    • 2013-09-25
    • 2019-07-30
    • 2019-05-18
    • 2017-07-13
    • 2013-10-07
    相关资源
    最近更新 更多