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 teacher : Form
    {
        public teacher()
        {
            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 = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
           
        }
        private void teacher_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("teachercode");
            comboBox2.Items.Add("fname");
            comboBox2.Items.Add("lname");


            comboBox1.Text = "=";
            comboBox2.Text = "teachercode";

            newMode = false;
            strSql = "SELECT * FROM teacher";
            con = new SqlConnection(strCon);
            con.Open();
            da = new SqlDataAdapter(strSql, con);
            da.Fill(ds, "teacher");
            textBox1.DataBindings.Add(new Binding("Text", ds, "teacher.teachercode"));
            textBox2.DataBindings.Add(new Binding("Text", ds, "teacher.fname"));
            textBox3.DataBindings.Add(new Binding("Text", ds, "teacher.lname"));
            textBox4.DataBindings.Add(new Binding("Text", ds, "teacher.rank1"));
            textBox5.DataBindings.Add(new Binding("Text", ds, "teacher.address1"));
            textBox6.DataBindings.Add(new Binding("Text", ds, "teacher.tel"));
            textBox7.DataBindings.Add(new Binding("Text", ds, "teacher.e_mail"));


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

       

        private void button1_Click(object sender, EventArgs e)
        {
            newRow = ds.Tables["teacher"].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["teachercode"] = textBox1.Text;
                newRow["fname"] = textBox2.Text;
                newRow["lname"] = textBox3.Text;
                newRow["rank1"] = textBox4.Text;
                newRow["address1"] = textBox5.Text;
                newRow["tel"] = textBox6.Text;
                newRow["e_mail"] = textBox7.Text;


                ds.Tables["teacher"].Rows.Add(newRow);
                cb = new SqlCommandBuilder(da);
                da = cb.DataAdapter;
                da.Update(ds, "teacher");
                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 teacher WHERE teachercode=" + sn;
            cmd.CommandText = sqlDel;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            ds.Clear();
            da.Fill(ds, "teacher");
            con.Close();
            MessageBox.Show("Delete is successful");
        }

        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 teacher WHERE " + comboBox2.Text + " " + comboBox1.Text + " " + "'" + textBox14.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(strSql, con);
            textBox16.Text = strSql;
            DataSet ds = new DataSet();
            da.Fill(ds, "teacher");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "teacher"));
            con.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {

            this.BindingContext[ds, "teacher"].Position = 0;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "teacher"].Position + 1); 
        }

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

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

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

        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, "teacher");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "teacher"));
            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')
                textBox3.Focus();  
        }

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

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

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

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

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

       
    }
}
