- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
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!