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 Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
        }
        
        DataBase mydatabase = new DataBase();
        //注意：下面两个变量ds,dt很重要，负责保留状态信息。
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();
        /// <summary>
        /// 更新数据信息
        /// </summary>
        public void updatData()
        {
            String sql = "select * from student";
            if (mydatabase.doUpdate(ds, sql, dt.TableName))
            {
                MessageBox.Show("数据更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("数据更新失败,请检查数据是否合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 初始化加载事件代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form8_Load(object sender, EventArgs e)
        {
            ds = mydatabase.GetDataSet("select * from student");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt.DefaultView;
        }
        /// <summary>
        /// 更新数据信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdata_Click(object sender, EventArgs e)
        {
            updatData();//更新数据
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("数据删除后将不可恢复，真的要删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                dataGridView1.Refresh();
                updatData();//更新数据
            }
        }
        /// <summary>
        /// 到头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected = false;
            dataGridView1.Rows[0].Selected = true;
            showmess();
        }
        /// <summary>
        /// 下一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            if (i < dataGridView1.Rows.Count - 1)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i + 1].Selected = true;
                showmess();

            }
            else
            {
                MessageBox.Show("已经是最后一项了！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 


        }
        /// <summary>
        /// 上一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            if (i != 0)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i - 1].Selected = true;
                showmess();
            }
            else
            {
                MessageBox.Show("已经是第一项了！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 

        }
        /// <summary>
        /// 到尾
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected = false;
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            showmess();
        }
        /// <summary>
        /// 底部显示信息
        /// </summary>
        private void showmess()
        {
            toolStripStatusLabel1.Text = string.Format("当前是第{0}条记录，共{1}条记录",dataGridView1.SelectedRows[0].Index+1,dataGridView1.Rows.Count);
            toolStripProgressBar1.Value = (int)((double)(dataGridView1.SelectedRows[0].Index) / dataGridView1.Rows.Count*100);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}