- [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
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [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
Tạo giao diện giống iPhone SMS với GridControl
Bài viết hôm nay không có gì quá đặt biệt chủ yếu giới thiệu các bạn làm quen với đối tượng GridControl trong bộ DevExpress thông qua một demo thiết kế giao diện giống trình SMS của iPhone. Trong bài viết này chủ yếu mình giới thiệu các bạn làm quen với sự kiện CustomDrawCell của GridView.
Trước tiên các bạn thiết kế giao diện như sau: (Các bạn nên download về để xem một vài thuộc tính trong GridControl và GridView)
Các sự kiện sử dụng trong bài viết này
gridView1.CustomDrawCell += gridView1_CustomDrawCell;
btnLeft.Click += btnLeft_Click;
btnRight.Click += btnRight_Click;
Load += Form1_Load;
Các hàm tạo dữ liệu và thêm DataSource cho GridControl
DataSet ds = new DataSet();
void InitData()
{
DataTable dt = new DataTable("dt");
DataColumn dcText = new DataColumn("Text", typeof(string));
DataColumn dcIndex = new DataColumn("Index", typeof(string));
dt.Columns.AddRange(new DataColumn[] { dcIndex, dcText });
ds.Tables.Add(dt);
dt.Rows.Add(new object[] { "L", "Hello! Laptrinhvb.net" });
dt.Rows.Add(new object[] { "R", "Hi. What's your name?" });
dt.Rows.Add(new object[] { "L", "My name Mr. Cùi Bắp" });
dt.Rows.Add(new object[] { "R", "Tên xấu vãi ^^" });
}
void InitGrid()
{
gridControl1.DataSource = ds.Tables[0];
gridView1.Columns[0].Visible = false;
gridView1.Columns[1].OptionsColumn.AllowEdit = false;
gridView1.Columns[1].OptionsColumn.ReadOnly = false;
gridControl1.ForceInitialize();
}
Quan trọng nhất trong bài viết này là sự kiện để vẽ lại cell trong GridView theo điều kiện mà mình quy định. Trong ví dụ này mình kiểm tra nếu chữ "L" thì vẽ image Left.png và ngược lại thì Right.png
void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
DataRow dr = gridView1.GetDataRow(e.RowHandle);
if (dr[0].ToString() == "L")
{
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
e.Graphics.DrawImage(new Bitmap(Application.StartupPath + @"left.png"), e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
Rectangle bounds = e.Bounds;
bounds.Offset(-30, 0);
e.Cache.DrawString(e.DisplayText, e.Appearance.Font, Brushes.Black, bounds, e.Appearance.GetStringFormat());
}
else
{
Rectangle bounds = e.Bounds;
bounds.Offset(30, 0);
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
e.Graphics.DrawImage(new Bitmap(Application.StartupPath + @"
ight.png"), e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
e.Cache.DrawString(e.DisplayText, e.Appearance.Font, Brushes.Black, bounds, e.Appearance.GetStringFormat());
}
e.Handled = true;
}
Sau cùng là những sự kiện khác như: FormLoad, btnLeft.Click, btnRight.Click
void btnRight_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows.Add(new object[] { "R", txtRight.Text });
txtRight.Text = string.Empty;
}
void btnLeft_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows.Add(new object[] {"L",txtLeft.Text });
txtLeft.Text = string.Empty;
}
void Form1_Load(object sender, EventArgs e)
{
InitData();
InitGrid();
}
Chúc các bạn vui với bài làm quen với đối tượng GridControl này. Nếu thấy thú vị hãy Like hoặc Shared page để khích lệ tinh thần để anh em tụi mình tiếp tục thực hiện những bài khác nhé. Xin cảm ơn!