- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
Hướng dẫn chèn hình ảnh vào gridcontrol (Insert Image Url Into GridView Devexpress use VB.NET)
Với Gridview mặc định của Winform thì chúng ta có thể chèn hình ảnh từ đường dẫn vào. Nhưng trong GridControl của Devexpress, nếu chúng ta muốn chèn hình ảnh vào GridControl, thì mặc định nó hỗ trợ đọc hình ảnh dưới dạng Base64, nếu hình ảnh được lưu vào database thì sẽ đọc được bình thường. Nhưng nếu hình ảnh mà lưu vào database thì sai quy tắc cơ bản của thiết kế dữ liệu, vì nếu lưu hình ảnh xuống database thì làm cho dung lượng dữ liệu của mình phình to và xử lý nặng nề hơn. Vì thế, hôm nay mình xin hướng dẫn các bạn chèn hình ảnh vào GridControl từ đường dẫn hình ảnh được lưu trên ổ đĩa.
Video chương trình demo:
- Để đọc được hình ảnh vào grid control chúng ta, cần tạo thêm ba file class sau để xử lý link hình ảnh:
- Đầu tiên tạo database Student với table test:
USE [Student]
GO
/****** Object: Table [dbo].[test] Script Date: 12/19/2015 08:41:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test](
[masv] [nvarchar](50) NOT NULL,
[tensv] [nvarchar](50) NULL,
[hinhanh] [nvarchar](350) NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED
(
[masv] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
1. GraphicsEdit.vb
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports GraphicsEditor.GraphicsEditor
Imports System.Data.SqlClient
Namespace CsWinFormsBlackApp
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
End Sub
Dim con As New SqlConnection
Public Sub Taoketnoi()
Dim str As String = "Data Source=.;Initial Catalog=Student;Integrated Security=True"
con.ConnectionString = str
con.Open()
End Sub
Public Function LayDulieu() As DataTable
Dim dt As New DataTable
Dim da As New SqlDataAdapter
da.SelectCommand = New SqlCommand("select masv as [Mã sinh viên], tensv as [Tên sinh viên], hinhanh as [Hình ảnh] from test", con)
da.Fill(dt)
Return dt
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Taoketnoi()
Dim dt As New DataTable
dt = LayDulieu()
GridControl1.DataSource = dt
Dim repItemGraphicsEdit As New RepositoryItemGraphicsEdit()
repItemGraphicsEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
repItemGraphicsEdit.BestFitWidth = 100
GridView1.Columns("Hình ảnh").ColumnEdit = repItemGraphicsEdit
End Sub
End Class
End Namespace
2. GraphicsEditViewInfo.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Drawing
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Repository
Namespace GraphicsEditor
Class GraphicsEditViewInfo
Inherits PictureEditViewInfo
Public Sub New(item As RepositoryItem)
MyBase.New(item)
End Sub
Public Overrides Property EditValue() As Object
Get
Return MyBase.EditValue
End Get
Set(value As Object)
If value IsNot Nothing AndAlso value.[GetType]() = GetType(System.String) Then
Try
MyBase.EditValue = New Bitmap(value.ToString())
Catch
MyBase.EditValue = Item.ErrorImage
End Try
Else
MyBase.EditValue = value
End If
End Set
End Property
End Class
End Namespace
3. RepositoryItemGraphicsEdit.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraEditors.Registrator
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Namespace GraphicsEditor
'The attribute that points to the registration method
_
Class RepositoryItemGraphicsEdit
Inherits RepositoryItemPictureEdit
' Static constructor should call registration method
Shared Sub New()
RegisterGraphicsEditor()
End Sub
Public Const GraphicsEditorName As String = "GraphicsEdit"
Public Overrides ReadOnly Property EditorTypeName() As String
Get
Return GraphicsEditorName
End Get
End Property
Public Shared Sub RegisterGraphicsEditor()
EditorRegistrationInfo.[Default].Editors.Add(New EditorClassInfo(GraphicsEditorName, GetType(GraphicsEdit), GetType(RepositoryItemGraphicsEdit), GetType(GraphicsEditViewInfo), New PictureEditPainter(), True))
End Sub
End Class
End Namespace
- Và cuối cùng là file chính của form1:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports GraphicsEditor.GraphicsEditor
Imports System.Data.SqlClient
Namespace CsWinFormsBlackApp
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
End Sub
Dim con As New SqlConnection
Public Sub Taoketnoi()
Dim str As String = "Data Source=.;Initial Catalog=Student;Integrated Security=True"
con.ConnectionString = str
con.Open()
End Sub
Public Function LayDulieu() As DataTable
Dim dt As New DataTable
Dim da As New SqlDataAdapter
da.SelectCommand = New SqlCommand("select masv as [Mã sinh viên], tensv as [Tên sinh viên], hinhanh as [Hình ảnh] from test", con)
da.Fill(dt)
Return dt
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Taoketnoi()
Dim dt As New DataTable
dt = LayDulieu()
GridControl1.DataSource = dt
Dim repItemGraphicsEdit As New RepositoryItemGraphicsEdit()
repItemGraphicsEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
repItemGraphicsEdit.BestFitWidth = 100
GridView1.Columns("Hình ảnh").ColumnEdit = repItemGraphicsEdit
End Sub
End Class
End Namespace
Chúc các bạn thành công. Mọi câu hỏi thắc mắc đến bài viết xin truy cập http://hoidap.laptrinhvb.net để được support.