NEWS

[Devexpress] Summary Specific Cells selected gridControl - Tính sum những ô được chọn trên lưới

[Devexpress] Summary Specific Cells selected gridControl - Tính sum những ô được chọn trên lưới
Đăng bởi: TONA Cody - Lượt xem: 8182 08:31:49, 14/04/2020C#   In bài viết

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.

Sum-selected-cells-gridcontrol-gridview-devexpress
Caption

 

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ị
        }

Download Source Code

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[Devexpress] Summary Specific Cells selected gridControl - Tính sum những ô được chọn trên lưới
Đăng bởi: TONA Cody - Lượt xem: 8182 08:31:49, 14/04/2020C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.