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_moreriat_mantaghe : 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_moreriat_mantaghe()
        {
            InitializeComponent();
        }

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

            da.Fill(ds, "b");

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

       

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

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

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

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txtmantaghe.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from manategh";
                da.Fill(ds, "n");
                if (ds.Tables["n"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from manategh";
                    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 manategh(mantaghe,code) values ('" + txtmantaghe.Text + "'," + i + " )";
                SqlCommand cmd = new SqlCommand(p, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from manategh order by code";
                da.Fill(ds, "b");
                DGmantaghe.DataSource = ds;
                DGmantaghe.DataMember = "b";
                txtmantaghe.Text = null;

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

        private void button1_Click_2(object sender, EventArgs e)
        {
            string y;
            y = "delete from manategh where mantaghe='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(y, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from manategh";
            da.Fill(ds, "h9");
            DGmantaghe.DataSource = ds;
            DGmantaghe.DataMember = "h9";
            DGmantaghe.Columns[0].HeaderText = "كد";
            DGmantaghe.Columns[1].HeaderText = "منطقه";
        }

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

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

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

        
    }
}