- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
[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!