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 grade : Form
    {
        public grade()
        {
            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 = "";
            

        }
        private void grade_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("stno");
            comboBox2.Items.Add("term");
            comboBox2.Items.Add("courseno");


            comboBox1.Text = "=";
            comboBox2.Text = "stno";

            newMode = false;
            strSql = "SELECT * FROM grade";
            con = new SqlConnection(strCon);
            con.Open();
            da = new SqlDataAdapter(strSql, con);
            da.Fill(ds, "grade");
            textBox1.DataBindings.Add(new Binding("Text", ds, "grade.stno"));
            textBox2.DataBindings.Add(new Binding("Text", ds, "grade.term"));
            textBox3.DataBindings.Add(new Binding("Text", ds, "grade.courseno"));
            textBox4.DataBindings.Add(new Binding("Text", ds, "grade.grade"));
            textBox5.DataBindings.Add(new Binding("Text", ds, "grade.teachercode"));
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "grade"));
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            newRow = ds.Tables["grade"].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["stno"] = textBox1.Text;
                newRow["term"] = textBox2.Text;
                newRow["courseno"] = textBox3.Text;
                newRow["grade"] = textBox4.Text;
                newRow["teachercode"] = textBox5.Text;
              
                ds.Tables["grade"].Rows.Add(newRow);
                cb = new SqlCommandBuilder(da);
                da = cb.DataAdapter;
                da.Update(ds, "grade");
                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 grade WHERE stno=" + sn;
            cmd.CommandText = sqlDel;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            ds.Clear();
            da.Fill(ds, "grade");
            con.Close();
            MessageBox.Show("Delete is successful");
        }

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

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

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

        private void button8_Click(object sender, EventArgs e)
        {
            int iCount;
            iCount = this.BindingContext[ds, "grade"].Count;
            this.BindingContext[ds, "grade"].Position = iCount;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "grade"].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 grade WHERE " + comboBox2.Text + " " + comboBox1.Text + " " + "'" + textBox14.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(strSql, con);
            textBox16.Text = strSql;
            DataSet ds = new DataSet();
            da.Fill(ds, "grade");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "grade"));
            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, "grade");
            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "grade"));
            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')
                button2.Focus();  
        }
    }
}
