- [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
[Devexpress] Summary Specific Cells selected gridControl - Tính sum những ô được chọn trên lưới
Hé lô mọi người ! Vì có một số bạn đã yêu cầu, nên hôm nay mình xin demo một project nhỏ thể hiện tính tổng các giá trị của các ô được chọn trên GridControl của DevExpress.
Phiên bản DevExpress của mình hiện tại là DevExpress.v19.2.5.

1. Đầu tiên mình thêm một gridControl từ Toolbox ra form. Để có được ô Summary trên lưới, mình cần thiết lập một số thuộc tính sau:
gridView1.OptionsSelection.MultiSelect = true;
gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
=> Nhằm cho phép gridView chọn nhiều ô trên lưới.
gridView1.OptionsView.ShowFooter = true;
=> Thiết lập này sẽ hiển thị phần footer của gridControl, ô Summary của gridView cũng sẽ hiển thị tại đây.
2. Tạo một DataTable generate một dữ liệu mẫu cho gridControl:
private static DataTable CreateTable(int RowCount)
{
Random r = new Random();
DataTable tbl = new DataTable();
tbl.Columns.Add("Name", typeof(string));
tbl.Columns.Add("ID", typeof(int));
tbl.Columns.Add("Value", typeof(int));
tbl.Columns.Add("State", typeof(bool));
tbl.Columns.Add("Date", typeof(DateTime));
for (int i = 0; i < RowCount; i++)
tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, r.Next(1, 99), i % 2, DateTime.Now.AddDays(i) });
return tbl;
}
Summary specific Cells selected gridControl
Tiếp theo, chúng ta viết trong event gridView1_SelectionChanged:
int totalSummary = 0;
private void gridView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
totalSummary = 0;
GridView view = sender as GridView;
if (view.FocusedColumn.FieldName == "Value")
{
GridCell[] seledtedCell = view.GetSelectedCells();
if (seledtedCell.Length > 0)
{
foreach (GridCell cel in seledtedCell)
{
totalSummary += (int)view.GetRowCellValue(cel.RowHandle, "Value");
}
}
}
else
{
for (int i = 0; i < view.RowCount; i++)
{
totalSummary += (int)view.GetRowCellValue(i, "Value");
}
}
gridView1.InvalidateFooter();
}
Trong hàm này, chúng ta xử lý những ô mà người dùng chọn. Lấy giá trị tổng và gán vào biến global.
Sau đó chúng ta viết hàm cho event gridView1_CustomDrawFooterCell:
if (e.Column.FieldName == "Value")
{
e.Info.DisplayText = totalSummary.ToString();
e.Column.SummaryItem.DisplayFormat = "Tổng: {0:N2}";
//BeginInvoke(new MethodInvoker(delegate { gridView1.UpdateTotalSummary(); }));
}
Do DevExpress không cho phép update giá trị ô Summary trực tiếp nên chúng ta phải update thông qua event này.
Notes:
- gridView1.InvalidateFooter();
- BeginInvoke(new MethodInvoker(delegate { gridView1.UpdateTotalSummary(); }));
2 dòng này có cùng một mục đích là tính toán và thiết lập lại giá trị của ô Summary trong gridControl. Chúng ta có thể sử dụng câu lệnh nào tùy ý, về hiệu năng thì mình không rõ, nhưng cá nhân mình nhận định nó cũng không ảnh hưởng gì nhiều đâu, nên thích xài cái nào cũng được. :D
Cuối cùng chúng ta gọi gán giá dataTable cho gridControl, như thế là xong.
public Form1()
{
InitializeComponent();
gridControl1.DataSource = CreateTable(10);
GridView view = gridView1;
for (int i = 0; i < view.RowCount; i++)
{
totalSummary += (int)view.GetRowCellValue(i, "Value");
}
//Chỗ này mình muốn tính totalSummary ngay từ đầu để k select thì vẫn có giá trị
}