- [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
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
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!