- [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] Hiển thị hình ảnh và text khi gridview không có dữ liệu
Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách vẽ chữ và hình ảnh khi không có dữ liệu trên GridView Devexpress C#.
[DEVEXPRESS] Empty data image gridview C#
Trong các ứng dụng điện thoại di động, các bạn thường thấy, khi sử dụng một danh sách Recycleview hay Listview trên android, khi không có dữ liệu, thì ứng dụng thì hiển thị ra một tấm hình empty data như hình dưới đây.
Trong bài viết này mình cũng sẽ làm tương tự với GridView Devexpress.
Khi lọc dữ liệu trên lưới GridView Devexpress mà không khớp dữ liệu thì mình sẽ hiển thị ra hình ảnh empty data như hình demo phần mềm dưới đây.
Full source Empty Image Gridview Devexpress C#:
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraLayout.Resizing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawEmptyStringGridView
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
CustomDrawEmptyForeground(gridControl1, gridView1);
var dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("address", typeof(string));
dt.Rows.Add(1, "Nguyễn Thảo", "Bà Rịa Vũng Tàu");
dt.Rows.Add(2, "Nguyễn Phương Nhi", "Hồ Chí Minh");
dt.Rows.Add(3, "Nguyễn Thị Cẩm Tú", "Kiên Giang");
dt.Rows.Add(4, "Nguyễn Đình Tuyên", "Quảng Bình");
dt.Rows.Add(5, "Cái Trí Minh", "Đồng Nai");
dt.Rows.Add(6, "Trịnh Quốc Khang", "Đồng Nai");
dt.Rows.Add(7, "Võ Sơn Băng", "Vĩnh Long");
dt.Rows.Add(8, "Hoàng Thị Thảo", "Quảng Bình");
gridControl1.DataSource = dt;
}
public void CustomDrawEmptyForeground(GridControl gridControl, GridView gridView)
{
string searchName = string.Empty;
Font noMatchesFoundTextFont = new Font("Tahoma", 10);
Font trySearchingAgainTextFont = new Font("Tahoma", 15, FontStyle.Underline);
Font trySearchingAgainTextFontBold = new Font(trySearchingAgainTextFont, FontStyle.Underline | FontStyle.Bold);
SolidBrush linkBrush = new SolidBrush(DevExpress.Skins.EditorsSkins.GetSkin(DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveLookAndFeel).Colors["HyperLinkTextColor"]);
string noMatchesFoundText = "Không tìm thấy dữ liệu, bạn ơi...";
string trySearchingAgainText = "Click to clear Filter";
Rectangle noMatchesFoundBounds = Rectangle.Empty;
Rectangle trySearchingAgainBounds = Rectangle.Empty;
bool trySearchingAgainBoundsContainCursor = false;
int offset = 10;
gridView.CustomDrawEmptyForeground += (s, e) =>
{
e.DefaultDraw();
e.Appearance.Options.UseFont = true;
e.Appearance.Font = noMatchesFoundTextFont;
Size size = e.Appearance.CalcTextSize(e.Cache, noMatchesFoundText, e.Bounds.Width).ToSize();
int x = (e.Bounds.Width - size.Width) / 2;
int y = e.Bounds.Y + offset;
noMatchesFoundBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, noMatchesFoundText, noMatchesFoundBounds);
e.Appearance.Font = trySearchingAgainBoundsContainCursor ? trySearchingAgainTextFontBold : trySearchingAgainTextFont;
size = e.Appearance.CalcTextSize(e.Cache, trySearchingAgainText, e.Bounds.Width).ToSize();
x = noMatchesFoundBounds.X - (size.Width - noMatchesFoundBounds.Width) / 2;
y = noMatchesFoundBounds.Bottom + offset;
size.Width += offset;
trySearchingAgainBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, trySearchingAgainText, trySearchingAgainBounds, linkBrush);
GridViewInfo vi = (GridViewInfo)gridView1.GetViewInfo();
var image = Image.FromFile("no-data.png");
var imageStartX = (vi.ViewRects.EmptyRows.X + (vi.ViewRects.EmptyRows.Width / 2)) - (image.Width / 2);
var imageStartY = (vi.ViewRects.EmptyRows.Y + (vi.ViewRects.EmptyRows.Height / 2)) - (image.Height / 2) + 40;
var rect = new Rectangle(imageStartX, imageStartY, image.Width, image.Height);
e.Graphics.DrawImage(image, rect);
};
gridView.MouseMove += (s, e) =>
{
trySearchingAgainBoundsContainCursor = trySearchingAgainBounds.Contains(e.Location);
gridControl.Cursor = trySearchingAgainBoundsContainCursor ? Cursors.Hand : Cursors.Default;
gridView.InvalidateRect(trySearchingAgainBounds);
};
gridView.MouseDown += (s, e) =>
{
if (trySearchingAgainBoundsContainCursor)
{
gridView1.ActiveFilter.Clear();
}
};
}
}
}
Thanks for watching!