- [C#] Ẩn ứng dụng winform từ Apps vào Background Process trên Task Manager
- [SQLSERVER] Xử lý ngoại lệ sử dụng TRY...CATCH và RAISEERROR
- [C#] Bắt sự kiện bàn phím chuột bên ngoài ứng dụng winform sử dụng thư viện MouseKeyHook
- [DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
- [C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
- [C#] Hướng dẫn thêm text vào hình ảnh icon winform
- [C#] Chia sẽ tổng hợp source code đồ án về Csharp
- [C#] Hướng dẫn viết ứng dụng quay màn hình video winform, Screen Recorder
- [C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual
- [DEVEXPRESS] Hướng dẫn tích Filter Contain khi click chuột phải vào cell selection trên Gridview
- [C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD
- [C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform
- [POWERSHELL] Script sao lưu backup và nén database sqlserver
- [C#] Giới thiệu thư viện Autofac Dependency Injection
- [C#] Hướng dẫn tạo Windows Services đơn giản Winform
- [C#] Một click chuột điều khiển máy tính từ xa sử dụng Ultraviewer
- Hướng dẫn đóng gói phần mềm sử dụng Powershell biên dịch script thành file exe
- [C#] Hướng dẫn sử dụng Task Dialog trên NET 5
- [C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Ultraviewer Winform
[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ị
}