เนื่องจากผมมีชื่อรูปใน DB โดยมีรูปแยกเก็บในตัวโฟร์เดอร์ต่างหากแล้ว หากผมต้องการดึง ชื่อรูปจาก db และดึงรูปจากโฟรเดอร์ไปแสดงผลใน Gridview ผมต้องใช้ลักษณคำสั่งแนวไหนครับ รบกวนขอคำชี้เเนะด้วยครับหาตัวอย่างไม่ได้เลย
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET, Windows
If dt.Rows.Count > 0 Then
find = True
' dt.Columns("productpic").DataType = System.Type.GetType("System.Byte[]")
'dt.Columns.Add("CarImageData", GetType(Byte()))
'For Each row As DataRow In dt.Rows
' row.SetField(Of Byte())("CarImageData", IO.File.ReadAllBytes(row.Field(Of String)("productpic")))
'Next
'*******************สร้าง Column เก็บรูปภาพใน DataGridView****************************************
dgv1.DataSource = dt
' If dgv1.Columns.Contains("ColImg") Then
With dgv1
.DefaultCellStyle.BackColor = Color.Azure
.AlternatingRowsDefaultCellStyle.BackColor = Color.White
.Columns("id").HeaderText = "ID"
.Columns("productname").HeaderText = "PRODUCT NAME"
.Columns("dateimport").HeaderText = "DATE IMPORT"
.Columns("dateexs").HeaderText = "DATE EXISTS"
.Columns("quantity").HeaderText = "QUANTITY"
.Columns("price").HeaderText = "PRICE"
.Columns("unitprice").HeaderText = "UNIT"
.Columns("productpic").HeaderText = "FILE PATH"
End With
' Else
'กำหนดความสูงของ Column
For Each row As DataGridViewRow In dgv1.Rows
If row.IsNewRow Then Continue For
row.Height = CInt(row.Height * 5)
Next
' Dim img As New DataGridViewImageCell
'สร้าง Column ไว้แสดงรูปภาพ
Dim ColImage As New DataGridViewImageColumn
If (dgv1.Columns.Contains("ColImg")) Then
Else
ColImage.Name = "ColImg"
ColImage.HeaderText = "PICTURE"
dgv1.Columns.Add(ColImage)
'ColImage.ImageLayout = DataGridViewImageCellLayout.Stretch
ColImage.ImageLayout = DataGridViewImageCellLayout.Zoom
End If
'ดึง Path ไฟล์เข้าไปใน DataGridview
For i As Integer = 0 To dt.Rows.Count - 1
Dim inImg As Image = Image.FromFile(dt.Rows(i)(7))
dgv1.Rows(i).Cells("ColImg").Value = inImg
Next
' End If
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim table = New DataTable()
Dim ImageColumn As New DataGridViewImageColumn
With ImageColumn
.Width = 15
.DefaultCellStyle.NullValue = Nothing
.ImageLayout = DataGridViewImageCellLayout.Zoom
End With
DataGridView1.Columns.Add(Nothing, "Id")
DataGridView1.Columns.Add(Nothing, "Product Name")
DataGridView1.Columns.Add(Nothing, "Picture")
DataGridView1.Columns.Add(ImageColumn)
'//create schema
table.Columns.Add("Id", GetType(Integer))
table.Columns.Add("ProductName", GetType(String))
table.Columns.Add("Picture", GetType(String))
'//add sample rows
table.Rows.Add(1, "N1", "DSCN6860")
table.Rows.Add(2, "N2", "DSCN6864")
table.Rows.Add(3, "N3", "DSCN6865")
table.Rows.Add(4, "N4", "DSCN6868")
Dim bmp As Bitmap
'Dim counter As Integer
Dim pPaht As String
For Each row As DataRow In table.Rows
pPaht = "C:\Users\Bundit\Pictures\" + row.Field(Of String)("Picture") + ".JPG"
bmp = New Bitmap(pPaht, True)
DataGridView1.Rows.Add(row.Field(Of Integer)("Id"), row.Field(Of String)("ProductName"), row.Field(Of String)("Picture"), bmp)
Next
End Sub
End Class
For Each row As DataRow In table.Rows
pPaht = "C:\Users\Bundit\Pictures\" + row.Field(Of String)("Picture") + ".JPG"
bmp = New Bitmap(pPaht, True)
DataGridView1.Rows.Add(row.Field(Of Integer)("Id"), row.Field(Of String)("ProductName"), row.Field(Of String)("Picture"), bmp)
Next
Server.MapPath specifies the relative or virtual path to map to a physical directory.
•Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
•Server.MapPath("..") returns the parent directory
•Server.MapPath("~") returns the physical path to the root of the application
•Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
An example:
Let's say you pointed a web site application (http://www.example.com/) to
C:\Inetpub\wwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342