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

namespace Library
{
    public partial class frmCategoryList : Form
    {
        public frmCategoryList()
        {
            InitializeComponent();
        }

        private void frmCategoryList_Load(object sender, EventArgs e)
        {
/*            category cat = new category();
            DataTable dt = new DataTable();
            dt = cat.GetList();
            dataGridView1.DataSource = dt;
            */
            
            category cat = new category();
            DataTable dt = cat.GetList();
            dataGridView1.DataSource = dt;
            /*
            category cat = new category();
            dataGridView1.DataSource = cat.GetList();


            dataGridView1.DataSource = new category().GetList();
             */


        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
                return;

            DialogResult dr;
            dr = MessageBox.Show("Are u Sure ?", "Delete", 
                                MessageBoxButtons.YesNo, 
                                MessageBoxIcon.Warning);

            if (dr == DialogResult.No)
                return;

            int row = dataGridView1.CurrentRow.Index;
            string val = dataGridView1[0, row].Value.ToString();
            category cat = new category();
            cat.ID = Int32.Parse(val);
            cat.delete();

            frmCategoryList_Load(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmCategoryAdd fa = new frmCategoryAdd();
            fa.ShowDialog();

            frmCategoryList_Load(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //1- Get Selected category ID From dataGridView
            int row = dataGridView1.CurrentRow.Index;
            string val = dataGridView1[0, row].Value.ToString();

            //2- Get Other Information From table By Select
            category cat = new category();
            cat.ID = Int32.Parse(val);
            cat.GetOne();
            //3- Create an Instance From Edit Form
            frmCategoryEdit fe = new frmCategoryEdit();
        
            //4- Read From Dt and Fill In Edit Form Textboxes
            fe.txtid.Text = val;
            fe.txtname.Text = cat.Name;

            //5- Show Edit Form
            fe.ShowDialog();

            //6- Load Data By Call Form Load Method
            frmCategoryList_Load(null, null);
        }
    }
}





