NEWS

[DEVEXPRESS] Hướng dẫn show Form chọn dữ liệu từ Form2 trả về Form1 trên C#

[DEVEXPRESS] Hướng dẫn show Form chọn dữ liệu từ Form2 trả về Form1 trên C#
Đăng bởi: Thảo Meo - Lượt xem: 3604 08:17:25, 24/03/2023DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn, cách tạo Form2 như hộp thoại dialog chọn thông tin Employee và trả về kết quả cho Form1 trên lập trình C# Winform.

[DEVEXPRESS] Get muli select value from Form Show Dialog Devexpress

Chi tiết các bạn có thể xem video Demo mình ở bên dưới:

Ở hình trên các bạn thấy, khi mình click vào button chọn Employee ở Form1.

Hệ thống sẽ hiển thị dữ liệu từ Form2 lên, và chúng ta sẽ chọn dữ liệu Employee, và khi bấm nút OK thì kết quả chọn sẻ trả về cho Form1.

Source code Form1.cs:

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 DemoGetValueFormDialog
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var frm = new FrmSelectEmployee();
          var dialogResult =   frm.ShowDialog();

            if (dialogResult == DialogResult.OK) {
                var data = frm.listEmployeeSelectedReturn;
                gridControl1.DataSource = data;
                // show this to Gridview
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            const string cacheKey = "Employees";
            var employees = FrmSelectEmployee.cache.Get(cacheKey) as List<Employee>;

            
            var b = employees;
        }
    }
}

Source code Form2.cs

using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoGetValueFormDialog
{
    public partial class FrmSelectEmployee : DevExpress.XtraEditors.XtraForm
    {
        public List<Employee> listEmployeeSelectedReturn = new List<Employee>();

        string URL_API = "https://raw.githubusercontent.com/dinhtona/api-mssql-dapper/main/db.json";
        public FrmSelectEmployee()
        {
            InitializeComponent();
            this.Load += FrmSelectEmployee_Load;
        }
        public static readonly MemoryCache cache = MemoryCache.Default;

        public  List<Employee> GetEmployees()
        {
            const string cacheKey = "Employees";
            var employees = cache.Get(cacheKey) as List<Employee>;

            if (employees == null)
            {
                employees = GetEmployeesFromAPI();
                cache.Add(cacheKey, employees, DateTimeOffset.Now.AddMinutes(30));
            }

            return employees;
        }

        public List<Employee> GetEmployeesFromAPI() {
            var dataEmployees = new WebClient().DownloadString(URL_API);
            var listEmployee = JsonConvert.DeserializeObject<List<Employee>>(dataEmployees);
            return listEmployee;
        }

        private void FrmSelectEmployee_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = GetEmployees();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var employeeSelected = gridView1.GetSelectedRows(); // this return rowHandle
            if (employeeSelected.Length == 0) {
                XtraMessageBox.Show("Please select an Employee.", "Message");
                return;
            }
            // get list employee

           
            foreach (var rowHandle in employeeSelected)
            {
                var employee = gridView1.GetRow(rowHandle) as Employee;
                listEmployeeSelectedReturn.Add(employee);
            }
            DialogResult= DialogResult.OK; // return OK
            this.Close();

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn show Form chọn dữ liệu từ Form2 trả về Form1 trên C#
Đăng bởi: Thảo Meo - Lượt xem: 3604 08:17:25, 24/03/2023DEVEXPRESS   In bài viết

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

Đọc tiếp
.