- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[DEVEXPRESS] Thêm menu Custom Display Format cho từng Grid Column trên Gridview
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 thêm chức năng Custom Format Display vào menu trên từng GridColumn của Gridview Devexpress C#, Winform.
[DEVEXPRESS] Custom Display Format Column Realtime in Gridview C# Winform
Với chức năng, giúp cho người dùng có thể dễ dàng tự động định dạng theo mong muốn của mình.
Giống chức năng Format Cell trong Microsoft Excel.
Hình ảnh demo ứng dụng Format Cell Gridview C#:
Đầu tiên, các bạn tạo cho mình một class với tên: FrmCustomDisplayFormat.cs
và thiết kế giao diện như mình bên dưới:
using DevExpress.Utils;
using System;
using System.Linq;
using System.Windows.Forms;
namespace CustomDisplayFormatGridView
{
public partial class FrmCustomDisplayFormat : DevExpress.XtraEditors.XtraForm
{
public FormatInfo FormatInfo;
public FrmCustomDisplayFormat(FormatInfo formatInfo, string columnName)
{
InitializeComponent();
FormatInfo = formatInfo;
this.Text = "Custom Display Format - " + columnName;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
var list_formattype = Enum.GetValues(typeof(FormatType))
.Cast<FormatType>()
.Select(v => v.ToString())
.ToList();
cb_formattype.Properties.DataSource = list_formattype;
cb_formattype.EditValue = FormatInfo.FormatType.ToString();
txt_formatstring.Text = FormatInfo.FormatString;
txt_formatstring.SelectAll();
txt_formatstring.Focus();
}
public T ToEnum<T>(string value, bool ignoreCase = true)
{
return (T)Enum.Parse(typeof(T), value, ignoreCase);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
FormatType format_type_selected = ToEnum<FormatType>(cb_formattype.EditValue.ToString());
FormatInfo.FormatType = format_type_selected;
FormatInfo.FormatString = txt_formatstring.Text;
this.Close();
}
private void FrmRenameCaption_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
}
}
Code sử dụng cho Form Main, chúng ta sẽ gắn vào sự kiện gridView1.PopupMenuShowing
Source code FormMain.cs
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CustomDisplayFormatGridView
{
public partial class FrmMain : DevExpress.XtraEditors.XtraForm
{
public FrmMain()
{
InitializeComponent();
gridView1.PopupMenuShowing += PopupMenuShowing;
}
public DataTable InitData()
{
var table = new DataTable();
table.Columns.Add("stt", typeof(int));
table.Columns.Add("product", typeof(string));
table.Columns.Add("mfg_date", typeof(DateTime));
table.Columns.Add("exp_date", typeof(DateTime));
table.Columns.Add("price", typeof(int));
table.Rows.Add(1, "Soup", new DateTime(2021, 10, 12, 12, 30, 05), new DateTime(2022, 8, 12), 15000);
table.Rows.Add(2, "Pomelo sweet soup", new DateTime(2021, 10, 12, 11, 13, 33), new DateTime(2022, 8, 12), 27000);
table.Rows.Add(3, "Baguette", new DateTime(2021, 7, 12, 06, 45, 11), new DateTime(2023, 8, 12), 34000);
table.Rows.Add(4, "Jackfruit yogurt", new DateTime(2021, 5, 12, 3, 45, 12), new DateTime(2021, 8, 12), 180000);
table.Rows.Add(5, "Cheese biscuits", new DateTime(2021, 8, 12, 12, 23, 9), new DateTime(2024, 8, 12), 32000);
table.Rows.Add(6, "Spaghetti", new DateTime(2021, 6, 12, 15, 20, 20), new DateTime(2025, 8, 12), 940000);
table.Rows.Add(7, "Sausages", new DateTime(2021, 12, 12, 23, 12, 8), new DateTime(2030, 8, 12), 12000);
table.Rows.Add(8, "Dessert wading in water", new DateTime(2021, 11, 12, 4, 30, 20), new DateTime(2021, 12, 12), 8000);
return table;
}
private void FrmMain_Load(object sender, EventArgs e)
{
var data = InitData();
gridControl1.DataSource = data;
}
public void PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
if (e.MenuType == GridMenuType.Column)
{
GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
// menu.Items.Clear();
if (menu.Column != null)
{
var display_format = new DXMenuCheckItem("Custom Display Format");
display_format.ImageOptions.Image = imageCollection1.Images[0];
display_format.Click += (ss, ee) =>
{
var displayFormat = menu.Column.DisplayFormat;
var frm = new FrmCustomDisplayFormat(displayFormat, menu.Column.Caption);
if (frm.ShowDialog() == DialogResult.OK)
{
menu.Column.ColumnEdit = new RepositoryItemTextEdit();
menu.Column.DisplayFormat.FormatType = frm.FormatInfo.FormatType;
menu.Column.DisplayFormat.FormatString = frm.FormatInfo.FormatString;
}
};
menu.Items.Add(display_format);
}
}
}
}
}
Thanks for watching!