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

namespace amlakyar
{
    public partial class frm_modirat_tabaghe : Form
    {
        static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\amlak.mdf;integrated security=true;User Instance=True");
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();


        string s1;
        public frm_modirat_tabaghe()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

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

        private void frm_modirat_tabaghe_Load(object sender, EventArgs e)
        {
            da.SelectCommand.CommandText = "select * from tabaghe order by code";

            da.Fill(ds, "b");

            DGtabaghe.DataSource = ds;
            DGtabaghe.DataMember = "b";
            DGtabaghe.Columns[0].HeaderText = "كد";
            DGtabaghe.Columns[1].HeaderText = "طبقه";
        }

        private void dg1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }

        private void DGtabaghe_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGtabaghe.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from tabaghe where code='" + s1 + "'";
            da.Fill(ds, "q");
            txtdel.Text = Convert.ToString(DGtabaghe.CurrentRow.Cells["name_tabaghe"].Value);

            textBox1.Text = Convert.ToString(DGtabaghe.CurrentRow.Cells["code"].Value);
        }

        private void DGtabaghe_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGtabaghe.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from tabaghe where code='" + s1 + "'";
            da.Fill(ds, "q");
            txtdel.Text = Convert.ToString(DGtabaghe.CurrentRow.Cells["name_tabaghe"].Value);

            textBox1.Text = Convert.ToString(DGtabaghe.CurrentRow.Cells["code"].Value);
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string p;

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txttabaghe.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from tabaghe";
                da.Fill(ds, "x");
                if (ds.Tables["x"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from tabaghe";
                    da.Fill(ds, "v");
                    dg1.DataSource = ds;
                    dg1.DataMember = "v";

                    i = Convert.ToInt16(dg1.CurrentRow.Cells["id"].Value);
                    i++;
                }
                else
                {
                    i = 1;
                }
                p = "insert into tabaghe(name_tabaghe,code) values ('" + txttabaghe.Text + "'," + i + " )";
                SqlCommand cmd = new SqlCommand(p, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from tabaghe order by code";
                da.Fill(ds, "b");
                DGtabaghe.DataSource = ds;
                DGtabaghe.DataMember = "b";
                txttabaghe.Text = null;

                MessageBox.Show("اطلاعات با موفقیت ثبت شد");

            }
            else { MessageBox.Show("لطفا تعداد طبقات را وارد کنید"); }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string j;
            j = "delete from tabaghe where name_tabaghe='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(j, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from tabaghe";
            da.Fill(ds, "h9");
            DGtabaghe.DataSource = ds;
            DGtabaghe.DataMember = "h9";
            DGtabaghe.Columns[0].HeaderText = "كد";
            DGtabaghe.Columns[1].HeaderText = "طبقه";

        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            string p;

            p = "update  tabaghe  set name_tabaghe='" + txttabaghe.Text + "' where code='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(p, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from tabaghe";
            da.Fill(ds, "x");
            DGtabaghe.DataSource = ds;
            DGtabaghe.DataMember = "x";
            txttabaghe.Text = null;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}