⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.cs

📁 学生管理(包括学生成绩 档案和课程) 学生可以录入数据
💻 CS
字号:
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 StudentManager
{
    public partial class Class : Form
    {
        private SqlConnection conn1 = null;
        private string selectstr = "Select * from Class ";
        private SqlDataAdapter sqlDataAdapter1 = null;
        private DataSet Dataset = new DataSet();
        private SqlCommand commond1 =new SqlCommand ();
        private SqlDataReader DataReader = null;
        private string commondstr;
        public Class()
        {
            InitializeComponent();
            conn1 = new SqlConnection(DataLevel.Connection.ConnString);
            sqlDataAdapter1 = new SqlDataAdapter(selectstr, this.conn1);
            sqlDataAdapter1.Fill(Dataset, "Class");
            Data_Bidings();
            Comdep_Bings();
            buttons_control(false);
            conn1.Close();
        }
        public void Data_Bidings()
        {
            dataGridView1.DataSource = Dataset;
            dataGridView1.DataMember = "Class";
            this.textClaID.DataBindings.Add("Text", this.Dataset, "Class.ClassID");
            this.textClaName.DataBindings.Add("Text", this.Dataset, "Class.ClassName");
            this.textStuNum.DataBindings.Add("Text", this.Dataset, "Class.StudentNumber");
            this.comboDep.DataBindings.Add("Text", this.Dataset, "Class.DepartmentName");
        }
        public void buttons_control(bool IsVisal)
        {
            if (IsVisal == true)
            {
                textClaID.Enabled = true;
                textClaName.Enabled = true;
                textStuNum.Enabled = true;
                comboDep.Enabled = true;
                btnApply.Enabled = true;
                btnCancel.Enabled = true;
            }
            else
            {
                this.btnCancel.Enabled = false;
                this.btnApply.Enabled = false;
                this.textClaID.Enabled = false;
                this.textClaName.Enabled = false;
                this.comboDep.Enabled = false;
                this.textStuNum.Enabled = false;
            }
        }
        public void Comdep_Bings()
        {
            
            if (conn1.State == ConnectionState.Closed)
                conn1.Open();
            commond1.Connection = conn1;
            commondstr = "Select DepartmentName from Class";
            this.commond1.CommandText = commondstr;
            DataReader = this.commond1.ExecuteReader();
            while (DataReader.Read())
            {
                this.comboDep.Items.Add(this.DataReader["DepartmentName"].ToString());
            }
            conn1.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.buttons_control(true);
            this.BindingContext[this.Dataset, "Class"].AddNew();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            this.buttons_control(true);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if(textClaID.Text=="")
            {
                MessageBox.Show("班级代码不能为空 !否则添加失败 !");
                return ;
            }
            this.BindingContext[this.Dataset,"Class"].EndCurrentEdit();
            if (conn1.State == ConnectionState.Closed)
                conn1.Open();
            SqlCommandBuilder commondbuilder1 = new SqlCommandBuilder(this.sqlDataAdapter1);
            sqlDataAdapter1.Update(this.Dataset, "Class");
            this.Dataset.AcceptChanges();
            dataGridView1.Refresh();
            buttons_control(false);
            conn1.Close();
       

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.BindingContext[this.Dataset, "Class"].Count > 0 &&
                (MessageBox.Show("真的要删除此记录吗 ?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(DialogResult.OK)))
            {
                int position = this.BindingContext[this.Dataset, "Class"].Position;
                this.BindingContext[this.Dataset, "Class"].RemoveAt(position);
            }
            if (conn1.State == ConnectionState.Closed)
                conn1.Open();
            SqlCommandBuilder sqlcommandbuilder = new SqlCommandBuilder(sqlDataAdapter1);
            sqlDataAdapter1.Update(this.Dataset, "Class");
            Dataset.AcceptChanges();
            dataGridView1.Refresh();
            buttons_control(false);
            conn1.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.BindingContext[this.Dataset, "Class"].CancelCurrentEdit();
            buttons_control(false);
        }

        private void CancelAll_Click(object sender, EventArgs e)
        {
            this.Dataset.RejectChanges();
            buttons_control(false);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -