- Tạo bản quyền phần mềm C# và bảo mật code | The Enigma Protector
- [C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform
- [C#] Lập trình ứng dụng lấy ngày giờ hệ thống mạng LAN sử dụng giao thức UDP
- [DATABASE] Sự khác nhau giữa hai câu lệnh TRUNCATE vs DELETE trong sqlserver
- [C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
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!