Kamis, 16 Desember 2010

Menampilkan gambar pada Datagridview

 









Try
objDataTable.Clear()
'menampilkan data pada table TbPic
'start

strSQL = "Select * from TbPic"

objCommand = New SqlCommand
objCommand.Connection = MyConnection.open
objCommand.CommandType = CommandType.Text
objCommand.CommandText = strSQL
objDataAdapter = New SqlDataAdapter(objCommand)
objDataAdapter.Fill(objDataSet, "MDT_Pic")
MyConnection.close()
objDataTable = objDataSet.Tables("MDT_Pic")
'finish
DataGridView1.DataSource = objDataSet
DataGridView1.DataMember = "MDT_Pic"

DataGridView1.ShowCellToolTips = False

' menyembunyikan binary data (Field Pic) DataGridView1.Columns("Pic").Visible = False

' Menambahkan image kolom pada gridview Dim ImageCol As New DataGridViewImageColumn()
ImageCol.Name = "Gambar"
ImageCol.Width = 200
DataGridView1.Columns.Add(ImageCol)

For Each Row As DataGridViewRow In DataGridView1.Rows
'pertama, kita harus mengconvert binary data menjadi memory stream
'Kemudian, kita dapat menggunakan memory stream untuk membuat image object

Try
Dim ImageBytes() As Byte = Row.Cells("Pic").Value

Dim ms As New MemoryStream(ImageBytes)
Dim img As Image = Image.FromStream(ms)


'lalu, ikat (bind) image kolom
Dim ImageCell As DataGridViewImageCell = CType(Row.Cells("Gambar"), DataGridViewImageCell)
ImageCell.Value = img

'Sekrang kita dapat merelease original data
Row.Cells("Pic").Value = New Byte() {}

Row.Height = 100
Catch
'abaikan error dari image yang invalid
End Try

Next

Catch When Err.Number <> 0
MsgBox("Program Error" _
& vbCrLf & Err.Description)
MyConnection.close()
End Try

Tidak ada komentar:

Posting Komentar