NEWS

[DEVEXPRESS] Sử dụng PopupContainerEdit và PopupContainerControl trong C# Winform

[DEVEXPRESS] Sử dụng PopupContainerEdit và PopupContainerControl trong C# Winform
Đăng bởi: Thảo Meo - Lượt xem: 7402 09:57:14, 02/10/2019DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn về cách sử dụng PopupContainerEdit và PopupContainerControl trong Devexpress C# winform.

[DEVEXPRESS] PopupContainerEdit và PopupContainerControl C#

Trong Devexpress, có hai control PopupContainerEditPopupContainerControl.

Với hai công cụ này, giúp chúng ta dễ dàng nhúng một control hay usercontrol hoặc một Form vào trong một Popup Control.

VD: Trong các control của Devexpress hiện tại là 19.1, khi chúng ta sử dụng GridLookupEdit, thì nó vẫn chưa cho chúng chọn nhiều dòng dữ liệu CheckBox như ở GridView.

Trong bài viết trước, mình đã có hướng dẫn cách custom lại để có thể chọn nhiều dòng dữ liệu trong GridLookupEdit

Tuy nhiên, trong bài viết này mình cũng hướng dẫn tương tự bằng cách mình sẽ nhúng luôn một GridView vào PopupContainerEdit.

Dưới đây là ví dụ mình ứng dụng hai control này để làm danh sách chọn các đơn hàng PO.

pop_up_container

Demo popup container c#

pop_up_container_csharp

Trong source code này, chúng ta cũng có thể nhúng nguyên một gridview trong trong Gridview.

Đầu tiên các bạn tạo cho mình một class customer.cs c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestGridLookUpEditCSV {
    public class Customer {
        public Customer() {
            this.ID = 0;
            this.Name = "";
            this.Info = "";
        }

        public Customer(int id, string name, string info) {
            this.ID = id;
            this.Name = name;
            this.Info = info;
        }

        public int ID { get; set; }
        public string Name { get; set; }
        public string Info { get; set; }
    }
}

Và tiếp tục là source code Form Main.cs

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestGridLookUpEditCSV {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public Form1() {
            InitializeComponent();
            InitGrid();
            RepositoryItemPopupContainerEdit ri = new RepositoryItemPopupContainerEdit();
            ri.QueryResultValue += ri_QueryResultValue;
            ri.EditValueChanged += ri_EditValueChanged;
            ri.Popup += ri_Popup;
            ri.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            PopupContainerControl popupControl = new PopupContainerControl();
            popupControl.Controls.Add(popupGridControl1);
            popupControl.Size = new System.Drawing.Size(500, 300);
            ri.PopupControl = popupControl;
            gridView1.Columns["Name"].ColumnEdit = ri;

            popupContainerEdit1.Properties.PopupControl = popupContainerControl1;
            popupContainerEdit1.Properties.QueryResultValue += Properties_QueryResultValue;
            popupContainerEdit1.Properties.EditValueChanged += Properties_EditValueChanged;
            popupContainerEdit1.Properties.Popup += Properties_Popup;

        }

        void Properties_Popup(object sender, EventArgs e) {
            PopupContainerEdit edit = sender as PopupContainerEdit;
            if(!edit.IsPopupOpen)
                edit.ShowPopup();
            UpdateSelection(edit, popupGridView2);
        }

        void Properties_EditValueChanged(object sender, EventArgs e) {
            PopupContainerEdit edit = sender as PopupContainerEdit;
            UpdateSelection(edit, popupGridView2);
        }

        void Properties_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e) {
            int[] selectedRows = popupGridView2.GetSelectedRows();
            List<string> values = new List<string>();
            foreach(int rowHandle in selectedRows) {
                values.Add(popupGridView2.GetRowCellValue(rowHandle, "Name").ToString());
            }
            string csv = String.Join(", ", values);
            e.Value = csv;
        }

        void ri_Popup(object sender, EventArgs e) {
            PopupContainerEdit edit = sender as PopupContainerEdit;
            UpdateSelection(edit, popupGridView1);
        }

        void ri_EditValueChanged(object sender, EventArgs e) {
            PopupContainerEdit edit = sender as PopupContainerEdit;
            if(!edit.IsPopupOpen)
                edit.ShowPopup();
            UpdateSelection(edit, popupGridView1);
        }

        void ri_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e) {
            int[] selectedRows = popupGridView1.GetSelectedRows();
            List<string> values = new List<string>();
            foreach(int rowHandle in selectedRows) {
                values.Add(popupGridView1.GetRowCellValue(rowHandle, "Name").ToString());
            }
            string csv = String.Join(", ", values);
            e.Value = csv;
        }

        private void UpdateSelection(PopupContainerEdit edit, GridView view) {
            view.BeginSelection();
            view.ClearSelection();
            if(edit != null)
                if(edit.EditValue != null) {
                    edit.Focus();
                    List<int> selection = GetSelection(edit.EditValue.ToString().Split(new string[] { ", " }, StringSplitOptions.None), "Name", view);
                    foreach(int rowHandle in selection) {
                        view.SelectRow(rowHandle);
                    }
                }
            view.EndSelection();
        }

        private List<int> GetSelection(string[] values, string fieldName, GridView view) {
            List<int> selection = new List<int>();
            foreach(string val in values) {
                for(int i = 0; i < view.RowCount; i++) {
                    if(view.GetRowCellValue(i, fieldName).ToString() == val)
                        selection.Add(i);
                }
            }
            return selection;
        }



        private void InitGrid() {
            popupGridControl1.DataSource = CreateList(5);
            gridControl2.DataSource = CreateList(5);
            popupGridControl2.DataSource = CreateList(5);
        }

        BindingList<Customer> CreateList(int count) {
            BindingList<Customer> list = new BindingList<Customer>();
            for(int i = 0; i < count; i++) {
                list.Add(new Customer(i, "Name" + i, "Info" + i));
            }
            return list;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Sử dụng PopupContainerEdit và PopupContainerControl trong C# Winform
Đăng bởi: Thảo Meo - Lượt xem: 7402 09:57:14, 02/10/2019DEVEXPRESS   In bài viết

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

Đọc tiếp
.