using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectManagementSystem.UI
{
    public partial class DataGridUI : UserControl, IDisplayUI
    {
        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置

        public DataGridUI()
        {
            InitializeComponent();
        }


        public virtual bool Init()
        {
            return true;
        }

        public virtual void UpdateDisplay()
        {
        }

        public void ShowDataGrid(object dataSource)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                this.SuspendLayout();
                if (dataGridView1.CurrentCell != null && dataGridView1.RowCount != 0 && dataGridView1.ColumnCount != 0)
                {
                    selectRowIndexOfordersDataGridView = dataGridView1.CurrentCell.RowIndex;
                    selectColumnIndexOfordersDataGridView = dataGridView1.CurrentCell.ColumnIndex;
                    scrollingRowIndexOfordersDataGridView = dataGridView1.FirstDisplayedScrollingRowIndex;
                    scrollingColumnIndexOfordersDataGridView = dataGridView1.FirstDisplayedScrollingColumnIndex;
                }

                this.dataGridView1.DataSource = dataSource;

                int n = dataGridView1.RowCount;
                int m = dataGridView1.ColumnCount;

                if (n != 0 && m != 0)
                {
                    if (selectRowIndexOfordersDataGridView < n && selectColumnIndexOfordersDataGridView < m &&
                        scrollingRowIndexOfordersDataGridView < n && scrollingColumnIndexOfordersDataGridView < m)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[selectRowIndexOfordersDataGridView].Cells[selectColumnIndexOfordersDataGridView];
                        dataGridView1.FirstDisplayedScrollingRowIndex = scrollingRowIndexOfordersDataGridView;
                        dataGridView1.FirstDisplayedScrollingColumnIndex = scrollingColumnIndexOfordersDataGridView;
                    }
                }

                this.ResumeLayout(false);
            }));
        }
    }
}

