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

📄 form1.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 Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
          
         
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //InitDataGridView(dgvStudent);
            BindALL(dgvStudent);
            
        }

        private void InitDataGridView(DataGridView dataGridView)
        {
            dataGridView.AllowUserToAddRows = true;
            dataGridView.ColumnCount = 5;

            dataGridView.Columns[0].HeaderText = "学号";
            dataGridView.Columns[1].HeaderText = "姓名";
            dataGridView.Columns[2].HeaderText = "年龄";
            dataGridView.Columns[3].HeaderText = "地址";
            dataGridView.Columns[3].HeaderText = "性别";
          
        
        }
        private void BindALL(DataGridView dataGridView)
        {
            StudentDAO Dao = new StudentDAO();
            Entitys = Dao.FindAll();
            dgvStudent.DataSource = entitys;
            //foreach (Student entity in Entitys )
            //{
            //    string[] row ={ entity.SNO, entity.SName, entity.Age.ToString (), entity.Address };
            //    dataGridView.Rows.Add(row );    
            
            //}
    
        }
        private IList<Student> entitys;
        public IList<Student> Entitys
        {
            get
            {
                return entitys;
            }
            set
            {

                entitys = value;
            }
        }

        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;

                }
                

            }
            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 void dgvStudent_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
          

        }

        private void dgvStudent_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridViewCell cell = dgvStudent.Rows[e.RowIndex].Cells[e.ColumnIndex];
         
            if (!CanSave(cell))
              
            {
                e.Cancel = true;
            }
        }

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

               }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormStudent form = FormStudent.getInstance();
            MessageBox.Show(form.Text1); 
            form.SetText(textBox1.Text);
            form.Invalidate();
        }

        
    }
}

⌨️ 快捷键说明

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