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

namespace WindowsApplication1
{
    public partial class Form12 : Form
    {
        public Form12()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        DataSet ds;
        /// <summary>
        /// 刷新窗体
        /// </summary>
        private void showmess()
        {
            ds = mydatabase.GetDataSet("select * from student");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            dataGridView1.Refresh();

        }
        /// <summary>
        /// 初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form12_Load(object sender, EventArgs e)
        {
            showmess();
        }
        /// <summary>
        /// 单击某个单元格时候发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 当用户点击链接时候发生.
            if (e.ColumnIndex == 0 && e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
            {
                object sno = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["sno"].Value;
                if (sno != null)
                {
                    this.ViewOrders(sno);
                }
            }
        }
        /// <summary>
        /// 获取sno并显示信息
        /// </summary>
        /// <param name="customerID"></param>
        private void ViewOrders(object sno)
        {
            //显示等待时候为鼠标指针沙漏图标
            this.Cursor = Cursors.WaitCursor;
            // 通过传递sno学生编号，显示该学生的全部成绩单信息.
            //首先建立新表单对象
            Form121 scform = new Form121(sno);
            scform.Show();
            //回复指针状态
            this.Cursor = Cursors.Default;

        }
        /// <summary>
        /// 主菜单的查看成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            object sno = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["sno"].Value;
            if (sno != null)
            {
                this.ViewOrders(sno);
            }
        }
        /// <summary>
        /// 主菜单的删除学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            object sno = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["sno"].Value;
            string sqlstring = "delete from student where sno="+sno.ToString();
            int i = mydatabase.ExecuteSQL(sqlstring);
            if (i > 0)
            {
                MessageBox.Show("该学生信息已经删除", "信息提示");
                showmess();
            }
            else
            {
                MessageBox.Show("删除信息异常，学生信息未删除", "信息提示");
            }
            
        }
        /// <summary>
        /// 主菜单的退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 主菜单的新增学生数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["sno"];
            dataGridView1.BeginEdit(false);
        }
        /// <summary>
        /// 快捷菜单：添加学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            toolStripMenuItem2_Click(sender, e);
        }
        /// <summary>
        /// 快捷菜单：删除学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            toolStripMenuItem8_Click(sender, e);
            //注意：此处学习事件的继承。
        }
        /// <summary>
        /// 快捷菜单： 查看学生成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
           toolStripMenuItem3_Click(sender,e);
           //注意：此处学习事件的继承。
        }
        /// <summary>
        /// 当在Grideview界面内点击鼠标右键时候发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            contextMenuStrip1.Show(this, new Point(e.X, e.Y));
        }
        /// <summary>
        /// 保存数据更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (mydatabase.doUpdate(ds, "select * from student", "table"))
            {
                MessageBox.Show("已经成功保存数据信息", "提示");
            }
            else
            {
                MessageBox.Show("保存数据信息失败", "提示");
            }
        }
        
        
    }
}