2016-02-02 145 views
0

我正在使用帶有2個字段的MySql數據庫:一個卷宗號和一個字段,用於保存圖像的名稱和位置。我想在我的winform上放置一個數據網格,並在第一個單元格中顯示一個圖像(縮略圖),在第二個單元格中顯示該文件的編號,但無法使其工作。從vb.net的數據庫中的數據庫中顯示圖像

圖片不顯示,只留下缺少的圖片鏈接。我也不能將圖片放在第一個單元格中。這是什麼樣子:

enter image description here

有什麼不對?

這是我寫的代碼:在你的代碼

Imports MySql.Data.MySqlClient 

Public Class Form1 


    'ALLES OM DATAGRID TE PLAATSEN 

    'GLOBALE DECLARATIES 
    Dim conString As String = "server=localhost;userid=root;database=testvehicle" 
    Dim con As New MySqlConnection(conString) 
    Dim cmd As MySqlCommand 
    Dim adapter As MySqlDataAdapter 
    Dim dt As New DataTable() 


    Private Sub Form1_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus 
     retrieve() 
    End Sub 



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     DataGridView1.ColumnCount = 2 
     DataGridView1.Width = 410 
     'CONSTRUCT IMAGE COLUMN 
     Dim imgCol As DataGridViewImageColumn = New DataGridViewImageColumn() 
     DataGridView1.Columns(0).Name = "Image" 
     DataGridView1.Columns.Add(imgCol) 
      'CONSTRUCT DATA COLUMNS 
     DataGridView1.Columns(1).Name = "dossiernr" 
     DataGridView1.Columns(1).Width = 100 
     DataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect 
    End Sub 

    'ALLES OM DATAGRID TE VULLEN 

    'PROCEDURE POPULATE ROW 
    Private Sub populate(foto1 As String, product_dossiernr As String) 
     Dim row As String() = New String() {foto1, product_dossiernr} 
     'ADD ROW TO ROWS 
     DataGridView1.Rows.Add(row) 
    End Sub 

'DATA ONTVANGEN 

    Private Sub retrieve() 
     DataGridView1.Rows.Clear() 
     'SQL STATEMENT 
     Dim sql As String = "SELECT foto1, product_dossiernr FROM producten ORDER BY product_dossiernr" 
     cmd = New MySqlCommand(sql, con) 
     'OPEN CON, RETRIEVE, FILL DATAGRID 
     Try 
      con.Open() 
      adapter = New MySqlDataAdapter(cmd) 
      adapter.Fill(dt) 
      For Each row In dt.Rows 
       populate(row(0), row(1)) 
      Next 
      con.Close() 
      dt.Rows.Clear() 
     Catch ex As Exception 
      MsgBox(ex.Message) 
      con.Close() 
     End Try 
    End Sub 

'EINDE DATAGRID VULLEN 

End Class 
+0

第一項** **相關**在右側:http://stackoverflow.com/q/2413476/1070452 – Plutonix

回答

0

它看起來像你命名你行的影像名稱,而不是分配一個圖像值所以這就是爲什麼你獲得的名稱,而不是圖片。

這是我以前使用的代碼片段,工作得很好。

Dim ColImage As New DataGridViewImageColumn 
Dim Img As New DataGridViewImageCell 

'Set Name 
ColImage.Name = "ColImg" 

'Set Header text 
ColImage.HeaderText = "Your Image!" 

'Add column to datagridview 
DataGridView1.Columns.Add(ColImage) 

'Set image value 
Img.Value = Image.FromFile("C:\YourImage.png") 

'Add the image cell to a row 
DataGridView1.Rows(0).Cells.Add(Img)