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

namespace Shopping.Extra
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = Shopping.Properties.Settings.Default["NikAmoozShop"].ToString();
                string query = " SELECT * FROM [HR].[EmployeeGroup]";
                SqlCommand cmd = new SqlCommand(query, conn);

                conn.Open();

                DataSet dsEmployeeGroup = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dsEmployeeGroup);

                if (dsEmployeeGroup.Tables[0].Rows.Count > 0)
                {
                    List<EmployeeGroup> Groups = new List<EmployeeGroup>();

                    foreach (DataRow dr in dsEmployeeGroup.Tables[0].Rows)
                    {
                        //Initialize Objects by Using an Object Initializer 
                        EmployeeGroup obj = new EmployeeGroup()
                        {
                            ID = Convert.ToInt16(dr["EmployeeGroupID"]),
                            Title = dr["EmployeeGroupTitle"].ToString()
                        };

                        /*
                        EmployeeGroup obj2 = new EmployeeGroup();
                        obj2.ID = 1;
                        obj2.Title = "";

                        EmployeeGroup obj3 = new EmployeeGroup() { ID = 2, Title = "" };
                         */ 

                        Groups.Add(obj);
                    }

                    gridEmployeeGroup.DataSource = Groups;
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("وقوع مشکل در هنگام کار با دیتایس" + "\n" + ex.Message, "خطای بانک اطلاعاتی", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show("وقوع مشکل در سیستم" + "\n" + ex.Message, "خطای سیستمی", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                conn.Close();
            }
        }
    }
}
