using System;
using System.Windows.Forms;
//---------------------------------------
using System.Data;
using System.Data.SqlClient;


namespace _17_6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strConn = "Server=.;" + "DataBase=MyTestDB;" +
                         "Integrated Security=True";

        string strDA = "Select * From Info";

        SqlConnection sCon;
        SqlDataAdapter sda;
        DataSet ds;
        DataView dv;
        CurrencyManager cm;

        private void FillDataSet()
        {
            sCon = new SqlConnection();
            sCon.ConnectionString = strConn;
            sda = new SqlDataAdapter(strDA, sCon);
            ds = new DataSet();
            sCon.Open();
            sda.Fill(ds, "table1");
            sCon.Close();
            
            dv = new DataView();
            dv.Table = ds.Tables["table1"];

            dataGridView1.DataSource = dv;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillDataSet();

            dataGridView1.RightToLeft = RightToLeft.Yes;
            dataGridView1.Columns[0].HeaderText = "شماره دانشجویی";
            dataGridView1.Columns[1].HeaderText = "نام خانوادگی";
            dataGridView1.Columns[2].HeaderText = "نام";
            dataGridView1.Columns[3].HeaderText = "جنسیت";
            dataGridView1.Columns[4].HeaderText = "معدل";
            dataGridView1.Columns[5].HeaderText = "تعداد واحد";

            comboBox1.Items.Add("شماره دانشجویی (نزولی)");
            comboBox1.Items.Add("نام خانوادگی");
            comboBox1.Items.Add("نام خانوادگی و سپس نام");
            comboBox1.Items.Add("نام");
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    dv.Sort = "Student_Number" + " " + "DESC";
                    break;
                case 1:
                    dv.Sort = "Last_Name";
                    break;
                case 2:
                    dv.Sort = "Last_Name, First_Name";
                    break;
                case 3:
                    dv.Sort = "First_Name";
                    break;
            }
            MessageBox.Show("Record Sorted");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            strDA = "Select * From Info Where Last_Name Like '%" + textBox1.Text + "%'";
            
            FillDataSet();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dv.RowFilter = "Number_of_Unit > 30" + " And " + "Average > 14";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FillDataSet();
            cm = (CurrencyManager)this.BindingContext[dv];
            dv.Sort = "Student_Number";
            int n = dv.Find(textBox2.Text);

            if (n >= 0)
                cm.Position = n;
            else
                MessageBox.Show("جستجو موفقیت‌آمیز نبود.");
        }
    }
}