- [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
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[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ị
}