NEWS

[DEVEXPRESS] Hiển thị hình ảnh và text khi gridview không có dữ liệu

[DEVEXPRESS] Hiển thị hình ảnh và text khi gridview không có dữ liệu
Đăng bởi: Thảo Meo - Lượt xem: 4363 13:32:42, 05/09/2020DEVEXPRESS   In bài viết

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.

empty_data_android

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.

empty_data_gridview

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!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hiển thị hình ảnh và text khi gridview không có dữ liệu
Đăng bởi: Thảo Meo - Lượt xem: 4363 13:32:42, 05/09/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.