- [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
[DEVEXPRESS] Hướng dẫn hiển thị hình ảnh flag country vào GridView sử dụng RepositoryItemTextEdit
Xin chào các bạn, bài viết hôm nay mình sẽ chia sẽ đến các bạn cách hiển thị hình ảnh vào Gridview trên Devexpress, giúp nhìn trực quan hơn.
[DEVEXPRESS] Load Image in Gridview C#
Khi các bạn, thiết kế ứng dụng, thì hình ảnh sẽ giúp cho người dùng nhìn ứng dụng một cách trực quan hơn.
Mình ví dụ: chúng ta có 1 danh sách các quốc gia.
Nhưng nếu chúng ta có hiện thị hình ảnh cờ của quốc gia đó lên kế bên, thì giúp người sử dụng nhìn vào thấy nhanh hơn.
Ở bài này, mình sẽ demo cho các bạn cách load các lá cờ theo quốc gia và hiển thị vào trên GridView Devexpress.
Giao diện demo ứng dụng hiển thị hình ảnh trên GridView:
Dưới đây mình có File mẫu data json, bao gồm các nước đang tham dự EURO 2021.
[
{
"id": 0,
"name": "Việt Nam",
"url": "https://laptrinhvb.net/uploads/flags/VIE.gif"
},
{
"id": 1,
"name": "Thổ Nhĩ Kỳ",
"url": "https://laptrinhvb.net/uploads/flags/TUR.gif"
},
{
"id": 2,
"name": "Ý",
"url": "https://laptrinhvb.net/uploads/flags/ITA.gif"
},
{
"id": 3,
"name": "Wales",
"url": "https://laptrinhvb.net/uploads/flags/WAL.gif"
},
{
"id": 4,
"name": "Thụy Sỹ",
"url": "https://laptrinhvb.net/uploads/flags/SUI.gif"
},
{
"id": 5,
"name": "Đan Mạch",
"url": "https://laptrinhvb.net/uploads/flags/DEN.gif"
},
{
"id": 6,
"name": "Phần Lan",
"url": "https://laptrinhvb.net/uploads/flags/FIN.gif"
},
{
"id": 7,
"name": "Bỉ",
"url": "https://laptrinhvb.net/uploads/flags/BEL.gif"
},
{
"id": 8,
"name": "Nga",
"url": "https://laptrinhvb.net/uploads/flags/RUS.gif"
},
{
"id": 9,
"name": "Áo",
"url": "https://laptrinhvb.net/uploads/flags/AUT.gif"
},
{
"id": 10,
"name": "Bắc Macedonia",
"url": "https://laptrinhvb.net/uploads/flags/MKD.gif"
},
{
"id": 11,
"name": "Hà Lan",
"url": "https://laptrinhvb.net/uploads/flags/NED.gif"
},
{
"id": 12,
"name": "Ukraine",
"url": "https://laptrinhvb.net/uploads/flags/UKR.gif"
},
{
"id": 13,
"name": "Anh",
"url": "https://laptrinhvb.net/uploads/flags/ENG.gif"
},
{
"id": 14,
"name": "Croatia",
"url": "https://laptrinhvb.net/uploads/flags/CRO.gif"
},
{
"id": 15,
"name": "Scotland",
"url": "https://laptrinhvb.net/uploads/flags/SCO.gif"
},
{
"id": 16,
"name": "CH Séc",
"url": "https://laptrinhvb.net/uploads/flags/CZE.gif"
},
{
"id": 17,
"name": "Ba Lan",
"url": "https://laptrinhvb.net/uploads/flags/POL.gif"
},
{
"id": 18,
"name": "Slovakia",
"url": "https://laptrinhvb.net/uploads/flags/SVK.gif"
},
{
"id": 19,
"name": "Tây Ban Nha",
"url": "https://laptrinhvb.net/uploads/flags/ESP.gif"
},
{
"id": 20,
"name": "Thụy Điển",
"url": "https://laptrinhvb.net/uploads/flags/SWE.gif"
},
{
"id": 21,
"name": "Hungary",
"url": "https://laptrinhvb.net/uploads/flags/HUN.gif"
},
{
"id": 22,
"name": "Bồ Đào Nha",
"url": "https://laptrinhvb.net/uploads/flags/POR.gif"
},
{
"id": 23,
"name": "Pháp",
"url": "https://laptrinhvb.net/uploads/flags/FRA.gif"
},
{
"id": 24,
"name": "Đức",
"url": "https://laptrinhvb.net/uploads/flags/GER.gif"
}
]
Full source code C#:
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ViewImageInGridView
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
List<Country> countries;
Dictionary<string, Image> dicImage;
public Form1()
{
InitializeComponent();
}
public class Country
{
public int id { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public void LoadImageFlag()
{
dicImage = new Dictionary<string, Image>();
var pic = new PictureBox();
foreach (var flag in countries)
{
pic.Load(flag.url);
dicImage.Add(flag.name, pic.Image);
}
}
private void Form1_Load(object sender, EventArgs e)
{
var jData = File.ReadAllText("countrys.json");
countries = JsonConvert.DeserializeObject<List<Country>>(jData);
LoadImageFlag();
gridControl1.DataSource = countries;
gridView1.CustomRowCellEdit += GridView1_CustomRowCellEdit;
}
private void GridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if(e.Column == colCountry)
{
var name = e.CellValue.ToString();
var id = countries.Where(x => x.name == name).FirstOrDefault().id - 1;
var textEditRespository = new RepositoryItemTextEdit();
var icon_flag = dicImage.Where(x => x.Key == name).FirstOrDefault().Value;
textEditRespository.ContextImageOptions.Image = icon_flag.GetThumbnailImage(16, 16, null, IntPtr.Zero);
// pictureEdit1.Image = icon_flag;
e.RepositoryItem = textEditRespository;
//((RepositoryItemTextEdit)e.RepositoryItem)
}
}
private void labelControl1_Click(object sender, EventArgs e)
{
}
}
}
Thanks for watching!