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 university
{
    public partial class city : Form
    {
        public city()
        {
            InitializeComponent();
        }
         bool newMode;
        DataSet ds = new DataSet();
        string strSql;
        string strCon = "SERVER=.;DATABASE=university;Integrated Security=True";
        SqlDataAdapter da;
        SqlConnection con;
        DataRow newRow;
        SqlCommandBuilder cb;
        private void empty()
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
        private void city_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("=");
            comboBox1.Items.Add(">");
            comboBox1.Items.Add(">=");
            comboBox1.Items.Add("<");
            comboBox1.Items.Add("<=");
            comboBox1.Items.Add("!=");

            comboBox2.Items.Add("city");
            comboBox2.Items.Add("name");



            comboBox1.Text = "=";
            comboBox2.Text = "city";

            newMode = false;
            strSql = "SELECT * FROM city";
            con = new SqlConnection(strCon);
            con.Open();
            da = new SqlDataAdapter(strSql, con);
            da.Fill(ds, "city");
            textBox1.DataBindings.Add(new Binding("Text", ds, "city.city"));
            textBox2.DataBindings.Add(new Binding("Text", ds, "city.name"));

            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "city"));
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            newRow = ds.Tables["city"].NewRow();
            newMode = true;

            empty();
            MessageBox.Show("Insert data and press save..");
            textBox1.Focus(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (newMode == true)
            {
                newRow["city"] = textBox1.Text;
                newRow["name"] = textBox2.Text;


                ds.Tables["city"].Rows.Add(newRow);
                cb = new SqlCommandBuilder(da);
                da = cb.DataAdapter;
                da.Update(ds, "city");
                con.Close();
                MessageBox.Show("Save is successful..");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sn;
            SqlCommand cmd = new SqlCommand();
            string sqlDel;
            con.Open();
            sn = textBox1.Text;
            sqlDel = "DELETE FROM city WHERE city=" + sn;
            cmd.CommandText = sqlDel;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            ds.Clear();
            da.Fill(ds, "city");
            con.Close();
            MessageBox.Show("Delete is successful");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "city"].Position = 0;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "city"].Position + 1); 
        }

       

        private void button7_Click(object sender, EventArgs e)
        {
            int iPos;
            int iCount;
            iPos = this.BindingContext[ds, "city"].Position + 1;
            iCount = this.BindingContext[ds, "city"].Count;
            if (iPos < iCount)
            {
                this.BindingContext[ds, "city"].Position += 1;
                textBox15.Text = Convert.ToString(this.BindingContext[ds, "city"].Position + 1);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int iPos;
            iPos = this.BindingContext[ds, "city"].Position + 1;
            if (iPos > 0)
            {
                this.BindingContext[ds, "city"].Position -= 1;
                textBox15.Text = Convert.ToString(this.BindingContext[ds, "city"].Position + 1);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int iCount;
            iCount = this.BindingContext[ds, "city"].Count;
            this.BindingContext[ds, "city"].Position = iCount;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "city"].Position + 1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.DataBindings.Clear();
            strCon = "SERVER=.;DATABASE=university;Integrated Security=True";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            if (textBox14.Text == "")
            {
                MessageBox.Show("جعبه متن خالی است کجایی همشهری ؟");
                textBox14.Focus();
            }
            else
                strSql = "SELECT * FROM city WHERE " + comboBox2.Text + " " + comboBox1.Text + " " + "'" + textBox14.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(strSql, con);
            textBox16.Text = strSql;
            DataSet ds = new DataSet();
            da.Fill(ds, "city");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "city"));
            con.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dataGridView1.DataBindings.Clear();
            strCon = "SERVER=.;DATABASE=university;Integrated Security=True";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            if (textBox16.Text == "")
            {
                MessageBox.Show("جعبه متن خالی است کجایی همشهری ؟");
                textBox14.Focus();
            }
            else
                strSql = textBox16.Text;
            SqlDataAdapter da = new SqlDataAdapter(strSql, con);

            DataSet ds = new DataSet();
            da.Fill(ds, "city");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "city"));
            con.Close();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch;
            ch = e.KeyChar;
            if (ch == '\r')
                textBox2.Focus();  
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch;
            ch = e.KeyChar;
            if (ch == '\r')
                button2.Focus();
        }
    }
}
