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

📄 formstudent.cs

📁 改程序能够查询课程表
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utility.Validate;

namespace StudentTest
{
    public partial class FormStudent : Form
    {
        private int newLine = 0;
        ComboBox cboSex = new ComboBox();
        public FormStudent()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 单例模式
        /// </summary>
        private static FormStudent instance;

        public static FormStudent getInstance()
        {
            if (instance == null)
            {
                instance = new FormStudent();
            }
            return instance;

        }

        public string Text1;
        public void SetText(string text)
        {
            textBox1.Text = text;
        }
        private void FormStudent_Load(object sender, EventArgs e)
        {
            BindALL(dgvStudent);
            initCombox();
            cboSex.Visible = false;
            dgvStudent.Controls.Add(cboSex);
            
        }

        private void initCombox()
        {
            DictionaryCodeDAO dao = new DictionaryCodeDAO();
            cboSex.DataSource = dao.FindByName("sex");
            cboSex.DisplayMember = "value";
            cboSex.ValueMember = "code";
          //  cboSex.SelectedIndexChanged += new EventHandler(cboSex_SelectedIndexChanged);
        
        }
        private void BindALL(DataGridView dataGridView)
        {
            IList<Student> Entitys;
            StudentDAO Dao = new StudentDAO();
            Entitys = Dao.FindAll();
            Dao.DeepLoad(Entitys,true );
            newLine = Entitys.Count;
            foreach (Student entity in Entitys)
            {
                string[] row ={ entity.SNO, entity.SName, entity.Age.ToString(), entity.Address ,entity.SexSource.Value   };
                dataGridView.Rows.Add(row);

            }

        }
        private void cboSex_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataGridViewRow dataGridViewRow = dgvStudent.CurrentRow;
            //Student entity = new Student();
            //entity.SNO = dataGridViewRow.Cells[0].Value != null ? dataGridViewRow.Cells[0].Value.ToString() : string.Empty;
            //entity.SName = dataGridViewRow.Cells[1].Value != null ? dataGridViewRow.Cells[1].Value.ToString() : string.Empty;
            //entity.Age = dataGridViewRow.Cells[2].Value != null ? int.Parse(dataGridViewRow.Cells[2].Value.ToString()) : 0;
            //entity.Address = dataGridViewRow.Cells[3].Value != null ? dataGridViewRow.Cells[3].Value.ToString() : string.Empty;
            //entity.Sex = cboSex.SelectedValue.ToString();
            //StudentDAO dao = new StudentDAO();
            //dao.Update(entity);
            //dataGridViewRow.Cells[4].Value = cboSex.Text;


        }
        private bool CanLeave(DataGridViewRow dataGridViewRow)
        {

            foreach (DataGridViewCell cell in dataGridViewRow.Cells)
            {
                if (cell.ColumnIndex == 0)//学号pk
                {
                    if (cell.Value == null || cell.Value == "" || cell.Value.ToString().Length > 8)
                    {
                        MessageBox.Show("学号不能为空,长度<9");
                        cell.Selected = true;
                        return false;

                    }


                }
            }
            return true;
        }
        private bool CanSave(DataGridViewCell cell)
        {

            if (cell.ColumnIndex == 0)//学号pk
            {
                if (cell.EditedFormattedValue == null || cell.EditedFormattedValue == "" || cell.EditedFormattedValue.ToString().Length > 8)
                {

                    MessageBox.Show("学号不能为空,长度<9");


                    cell.Selected = true;
                    return false;

                }
                else
                {
                    foreach (DataGridViewRow row in dgvStudent.Rows)
                    {
                        if (row.Index != cell.RowIndex && row.Index != dgvStudent.NewRowIndex)
                        {
                            if (cell.EditedFormattedValue.ToString() == row.Cells[0].Value.ToString())
                            {
                                MessageBox.Show("学号重复");
                                return false;
                            }

                        }
                    }
                }


            }
               
                if (cell.ColumnIndex == 1)//姓名
                {
                    if (cell.EditedFormattedValue.ToString().Length > 20)
                    {

                        MessageBox.Show(" 姓名长度<20");
                        cell.Selected = true;
                        return false;
                    }
                }

                if (cell.ColumnIndex == 2)//年龄
                {
                    if (!TypeValidate.IsInteger(cell.EditedFormattedValue.ToString()))
                    {

                        MessageBox.Show(" 输入整型数据");
                        return false;

                    }
                }
          
            if (cell.ColumnIndex == 3)//地址
            {
                if (cell.EditedFormattedValue.ToString().Length > 50)
                {

                    cell.Selected = true;
                    return false;

                }


            }
            return true;

        }

        private bool CanLeave1(DataGridViewRow dataGridViewRow)
        {

            foreach (DataGridViewCell cell in dataGridViewRow.Cells)
            {
                if (cell.ColumnIndex == 0)//学号pk
                {
                    if (cell.Value == null || cell.Value == "" || cell.Value.ToString().Length > 8)
                    {
                        
                        cell.Selected = true;
                        return false;

                    }


                }
            }
            return true;
        }
        private void dgvStudent_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {

            if (dgvStudent.CurrentRow.IsNewRow) return;
            DataGridViewCell cell = dgvStudent.Rows[e.RowIndex].Cells[e.ColumnIndex];

            if (!CanSave(cell))
            {
                e.Cancel = true;
            }
        }

        private void dgvStudent_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvStudent.CurrentRow.IsNewRow) return;
            DataGridViewRow dataGridViewRow = dgvStudent.Rows[e.RowIndex];
            if (!CanLeave(dataGridViewRow))
            {
                e.Cancel = true;

            }
        }

        private void dgvStudent_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            StudentDAO dao = new StudentDAO();
            DataGridViewRow dataGridViewRow = dgvStudent.Rows[e.RowIndex];
            if (e.RowIndex == newLine )//新增
            {
           
                if (CanLeave1(dataGridViewRow))
                {
                    Student entity = new Student();
                    entity.SNO = dataGridViewRow.Cells[0].Value != null ? dataGridViewRow.Cells[0].Value.ToString() : string.Empty;
                    entity.SName = dataGridViewRow.Cells[1].Value != null ? dataGridViewRow.Cells[1].Value.ToString() : string.Empty;
                    entity.Age = dataGridViewRow.Cells[2].Value != null ? int.Parse(dataGridViewRow.Cells[2].Value.ToString()) : 0;
                    entity.Address = dataGridViewRow.Cells[3].Value != null ? dataGridViewRow.Cells[3].Value.ToString() : string.Empty;
                    
                    dao.Insert(entity);
                    newLine++;

                }
            }
            else
            {
                Student entity = new Student();
                entity.SNO = dataGridViewRow.Cells[0].Value != null ? dataGridViewRow.Cells[0].Value.ToString() : string.Empty;
                entity.SName = dataGridViewRow.Cells[1].Value != null ? dataGridViewRow.Cells[1].Value.ToString() : string.Empty;
                entity.Age = dataGridViewRow.Cells[2].Value != null ? int.Parse(dataGridViewRow.Cells[2].Value.ToString()) : 0;
                entity.Address = dataGridViewRow.Cells[3].Value != null ? dataGridViewRow.Cells[3].Value.ToString() : string.Empty;
                dao.Update(entity);
            
            }


        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvStudent.CurrentRow.IsNewRow)return  ;
            string SNO = dgvStudent.CurrentRow.Cells[0].Value.ToString();
            StudentDAO dao = new StudentDAO();
            dao.DeleteByPK(SNO);
            dgvStudent.Rows.RemoveAt(dgvStudent.CurrentRow.Index );
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Text1 = textBox1.Text;
            Form1 form1 = new Form1();

            form1.ShowDialog();
        }

        private void FormStudent_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }
        /// <summary>
        /// 设计控件在DataGrid中的位置
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="control"></param>
        private void SetControlRect(Rectangle rect, Control control)
        {
            control.Left = rect.Left;
            control.Top = rect.Top;
            control.Width = rect.Width;
            control.Height = rect.Height;

        }
        private void dgvStudent_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudent.Rows.Count > 0)
            {
                Rectangle rect = dgvStudent.GetCellDisplayRectangle(dgvStudent.CurrentCell.ColumnIndex, dgvStudent.CurrentCell.RowIndex, false);
                if (dgvStudent.CurrentCell.ColumnIndex == 4)
                {
                    SetControlRect(rect, cboSex);
                    cboSex.Visible = true;


                }
                else
                {
                    cboSex.Visible = false;
                }
            }

        }

        private void dgvStudent_Resize(object sender, EventArgs e)
        {
            cboSex.Visible = false;
        }

        private void dgvStudent_Scroll(object sender, ScrollEventArgs e)
        {
            cboSex.Visible = false;
        }

        private void dgvStudent_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {


                DataGridViewRow dataGridViewRow = dgvStudent.CurrentRow;
                Student entity = new Student();
                entity.SNO = dataGridViewRow.Cells[0].Value != null ? dataGridViewRow.Cells[0].Value.ToString() : string.Empty;
                entity.SName = dataGridViewRow.Cells[1].Value != null ? dataGridViewRow.Cells[1].Value.ToString() : string.Empty;
                entity.Age = dataGridViewRow.Cells[2].Value != null ? int.Parse(dataGridViewRow.Cells[2].Value.ToString()) : 0;
                entity.Address = dataGridViewRow.Cells[3].Value != null ? dataGridViewRow.Cells[3].Value.ToString() : string.Empty;
                entity.Sex = cboSex.SelectedValue.ToString();
                StudentDAO dao = new StudentDAO();
                dao.Update(entity);
                dataGridViewRow.Cells[4].Value = cboSex.Text;
            
            }
        }
    }
}

⌨️ 快捷键说明

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