最后一本书 第六章 上机

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 最后一本书_第六章_上机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        { //
        //    string message = string.Format("选中{0}节点,深度是{1}", treeView1.SelectedNode.Text, treeView1.SelectedNode.Level);
        //    MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            try
            {
                if (treeView1.SelectedNode!=null)
                {
                    DataView dv = new DataView(ds.Tables["student"]);
                    
                if (treeView1.SelectedNode.Level == 1)
                {
                    string gradeName = treeView1.SelectedNode.Text;
                    dv.RowFilter = string.Format("GradeName='{0}'", gradeName);//筛选条件
                }else if(treeView1.SelectedNode.Level == 2){
                    string sex = treeView1.SelectedNode.Text;
                    dv.RowFilter = string.Format("GradeName='{0}' and sex='{1}'",treeView1.SelectedNode.Parent.Text,sex);
                }
                dv.Sort="StudentNo";
                    dataGridView1.DataSource = dv;//从新绑定数据
                }else{
                MessageBox.Show("请选择节点");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

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

        string sql = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
        public void ss(){
            string stu = "SELECT * FROM Student a,Grade b where a.GradeId=b.GradeId";
            SqlConnection conn = new SqlConnection(sql);
            SqlDataAdapter da = new SqlDataAdapter(stu,conn);
            try
            {if(ds.Tables["stu"]!=null){
                ds.Tables["stu"].Clear();
            }
                da.Fill(ds,"student");
                dataGridView1.DataSource=ds.Tables["student"];
            }
            catch (Exception e)
            {
                
                MessageBox.Show(e.Message);
            }

        }
        public void ll() {
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            ss();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count>0){
                DialogResult c=MessageBox.Show("确定要删除该学生吗?同时会删除该学生的成绩!","提示",MessageBoxButtons
                    .YesNo,MessageBoxIcon.Warning);
                if(c==DialogResult.Yes){
                    string studentNo = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
                   
                    string s = string.Format(@"delete  from Result where studentNo='{0}';
                                                 delete  from Student where studentNo='{0}'",studentNo);
                    SqlConnection conn = new SqlConnection(sql);
                    try
                    {
                        conn.Open();
                        SqlCommand comm = new SqlCommand(s,conn);
                        int i = comm.ExecuteNonQuery();
                        if (i > 0)
                        {

                            MessageBox.Show("删除成功!", "提示");
                            ss();
                        }
                        else {
                            MessageBox.Show("删除失败!", "提示");
                        }
                        

                    }
                    catch (Exception es)
                    {

                        MessageBox.Show(es.Message);
                    }
                    finally {
                        conn.Close();
                    }
                }
            }
        }

        private void 获得学号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string studentNo1 = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);
            MessageBox.Show("学号为:" + studentNo1);
        }
    }
}

你可能感兴趣的:(最后一本书 第六章 上机)