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 Form3 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=javaher;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        DataTable dtable = new DataTable();
        public static DataGridView dv;
        public Form3()
        {
            InitializeComponent();
        }

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

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

        private void Form3_Load(object sender, EventArgs e)
        {
            dv = dataGridView1;
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select type from javaher";
            SqlDataReader dr = cmd.ExecuteReader();
            dt.Columns.Add("type");
            dt.Load(dr);
            cmbType.DataSource = dt;
            cmbType.ValueMember = "type";
            cmbType.DisplayMember = "type";
            con.Close();
            //***
            showgrid();
            //***
            dataGridView1.Columns[0].HeaderText = "کد";
            dataGridView1.Columns[1].HeaderText = "نوع جواهر";
            dataGridView1.Columns[2].HeaderText = "نام جواهر";
            dataGridView1.Columns[3].HeaderText = "شرکت تولیدی";
            dataGridView1.Columns[4].HeaderText = "قیمت";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "" || txtPrice.Text == "")
                MessageBox.Show("فیلدهای نام جواهر یا قیمت خالی است", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "insert into [kala] (type,name,company,price) values ('" + cmbType.SelectedValue.ToString() + "','" + txtName.Text.Trim() + "','" + txtCompany.Text.Trim() + "','" + txtPrice.Text.Trim() + "')";
                cmd.ExecuteNonQuery();
                MessageBox.Show("ثبت با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();
                showgrid();
                txtName.Text = txtCompany.Text = txtPrice.Text = "";
                txtName.Focus();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            string id=dataGridView1[0,row].Value.ToString();
            string name = dataGridView1[2, row].Value.ToString();
            string company = dataGridView1[3, row].Value.ToString();
            string price = dataGridView1[4, row].Value.ToString();
            EditKala ek = new EditKala();
            ek.lblID.Text = id;
            ek.txtName.Text = name;
            ek.txtCompany.Text = company;
            ek.txtPrice.Text = price;
            ek.ShowDialog();
        }

        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 [kala] where id='"+txtcode.Text.Trim()+"'";
                cmd.ExecuteNonQuery();
                MessageBox.Show("حذف با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();
                showgrid();
                txtcode.Text = "";
            }
        }
    }
}
