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;
using System.Data.SqlClient;

namespace Javaher
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=javaher;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        DataTable dtable = new DataTable();
        public static DataGridView dv;
        public Form2()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        void showgrid()
        {
            dtable.Clear();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select * from javaher";
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dtable);
            dataGridView1.DataSource = dtable;
            con.Close();
        }

        public void Form2_Load(object sender, EventArgs e)
        {
            dv = dataGridView1;
            showgrid();
            dataGridView1.Columns[0].HeaderText = "کد نوع";
            dataGridView1.Columns[1].HeaderText = "نوع جواهر";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (txtType.Text == "")
            {
                MessageBox.Show("لطفا نوع جواهر را وارد نمایید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtType.Focus();
            }
            else
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "insert into [javaher] ([type]) values ('"+txtType.Text.Trim()+"')";
                cmd.ExecuteNonQuery();
                MessageBox.Show("ثبت با موفقیت انجام شد","",MessageBoxButtons.OK,MessageBoxIcon.Information);
                con.Close();
                txtType.Text = "";
                txtType.Focus();
                showgrid();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtCode.Text == "")
            {
                MessageBox.Show("لطفا کد نوع جواهر را وارد نمایید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCode.Focus();
            }
            else
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "delete from [javaher] where ([id]='"+txtCode.Text.Trim()+"')";
                //***********************************
                SqlCommand cmdread = new SqlCommand();
                cmdread.Connection = con;
                cmdread.CommandText = "select * from javaher where ([id]='" + txtCode.Text.Trim() + "')";
                //***********************************
                SqlDataReader dr = cmdread.ExecuteReader();

                if (dr.Read() == true)
                {
                    dr.Close();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("حذف با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                else
                    MessageBox.Show("موردی برای حذف وجود ندارد","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
                con.Close();
                txtCode.Text = "";
                txtCode.Focus();
                showgrid();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            string valid = dataGridView1[0, row].Value.ToString();
            string valtype = dataGridView1[1, row].Value.ToString();
            EditFrm ef = new EditFrm();
            ef.txtCode.Text = valid;
            ef.txtType.Text = valtype;
            ef.ShowDialog();
        }
    }
}
