using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'universityDataSet.Students' table. You can move, or remove it, as needed.
            this.studentsTableAdapter.Fill(this.universityDataSet.Students);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ReadOnly = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.Value.ToString().Trim() == "حذف رکورد")
            {
                dataGridView1.ReadOnly = false;
                dataGridView1.AllowUserToDeleteRows = true;
                string conStr, sqlStr;
                conStr = "Data Source=va-pc;Initial Catalog=University;Integrated Security=True";
                SqlConnection connect = new SqlConnection(conStr);
                connect.Open();
                SqlCommand cmd = new SqlCommand();
                string stNo = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                sqlStr = "delete from Students where studentId = " + stNo;
                cmd.CommandText = sqlStr;
                cmd.Connection = connect;
                cmd.ExecuteNonQuery();
                this.universityDataSet.Students.Clear();
                this.studentsTableAdapter.Fill(this.universityDataSet.Students);
                dataGridView1.Update();
                connect.Close();
                dataGridView1.ReadOnly = true;
            }
        }
    }
}
