GridViewコントロールの追加削除(検索)

8223 ワード

全部で6種類~
モデルレイヤー
データ・アクセス・レイヤStudentServices,GradeServices
論理処理層StudentManager、GradeManager
UI層のバインド方法1.7タイトル位置
1.3 DALについて.StudentServicesクラスの後ろにあるGenderフィールドの詳細は、三元演算子の構文を検索できます.
このうち、コードはセキュリティを考慮せずに学習の参考にします.
書き込みなしの戻りタイプのメソッドは、パラメータなしとパラメータ付きのコンストラクション関数です.
使用方法:
Student stu = new Student();
Student stu = new Student("               、    ");

1.1 Model.Studioエンティティクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyWebProjectModel
{
    public class Student
    {
        /// 
        ///   
        /// 
        public string StudentNo { get; set; }
        /// 
        ///   
        /// 
        public string LoginPwd { get; set; }
        /// 
        ///   
        /// 
        public string StudentName { get; set; }
        /// 
        ///   
        /// 
        public string Gender { get; set; }
        /// 
        ///   
        /// 
        public int GradeId { get; set; }
        /// 
        ///   
        /// 
        public string Phone { get; set; }
        /// 
        ///   
        /// 
        public string Address { get; set; }
        /// 
        ///   
        /// 
        public DateTime BornDate { get; set; }
        /// 
        ///   
        /// 
        public string Email { get; set; }
        public string GradeName { get; set; }
        /// 
        ///    
        /// 
        public string IdentityCard { get; set; }
        public Student() { }
        /// 
        ///       
        /// 
        ///   
        ///   
        ///   
        ///   
        ///   
        ///   
        ///   
        ///   
        ///   
        ///    
        public Student(string StudentNo, string LoginPwd, string StudentName, string Gender, int GradeId, string Phone, string Address, DateTime BornDate, string Email, string IdentityCard)
        {
            this.StudentNo = StudentNo;
            this.LoginPwd = LoginPwd;
            this.StudentName = StudentName;
            this.Gender = Gender;
            this.GradeId = GradeId;
            this.Phone = Phone;
            this.Address = Address;
            this.BornDate = BornDate;
            this.Email = Email;
            this.IdentityCard = IdentityCard;
        }
    }
}

1.2 Model.Gradeエンティティークラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyWebProjectModel
{
    public class Grade
    {
        public string GradeId { get; set; }
        public string GradeName { get; set; }
    }
    public Grade(){}
    public Grade(string GradeId,string GradeName)
    {
	this.GradeId = GradeId;
	this.GradeName = GradeName;
    }
}
 
  

1.3 DAL.StudentServices

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectModel;
using System.Data.SqlClient;

namespace MyWebProjectDAL
{
    public class StudentServices
    {
        public List GetStudentAll()
        {
            List list = new List();
            string sql = "select * from Student";
            using (SqlDataReader reader = DBHelper.GetExecuteReader(sql))
            {
                while (reader.Read())
                {
                    Student stu = new Student()
                    {
                        StudentNo = reader["StudentNo"].ToString(),
                        LoginPwd = reader["loginPwd"].ToString(),
                        StudentName = reader["studentName"].ToString(),
                        Gender = Convert.ToBoolean(reader["gender"]) == true ? " " : " ",
                        GradeId = int.Parse(reader["gradeId"].ToString()),
                        GradeName = new GradeServices().GetGradeById(Convert.ToInt32(reader["gradeId"])),
                        Phone = reader["phone"].ToString(),
                        Address = reader["address"].ToString(),
                        BornDate = Convert.ToDateTime(reader["bornDate"]),
                        Email = reader["email"].ToString(),
                        IdentityCard = reader["identityCard"].ToString()
                    };
                    list.Add(stu);
                }
                return list;
            }
        }
    } 
}

1.4 DAL.GradeServicesクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyWebProjectModel;

namespace MyWebProjectDAL
{
    public class GradeServices
    {
	public List GetGradeAll()
        {
            List list = new List();
            string sql = "select * from Grade";
            using (SqlDataReader reader = DBHelper.GetExecuteReader(sql))
            {
                while (reader.Read())
                {
                    Grade gd = new Grade()
                    {
                        GradeId = reader["GradeId"].ToString(),
                        GradeName = reader["gradeName"].ToString()
                    };
                    list.Add(gd);
                }
            }
            return list;
        }
    }
}

1.5 BLL.StudentManagerクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectDAL;
using MyWebProjectModel;


namespace MyWebProjectBLL
{
    public class StudentManager
    {


        StudentServices ss = new StudentServices();
        public List GetStudentAll()
        {
            return ss.GetStudentAll();
        }
    }
}
 
   
  


1.6 BLL.GradeManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectModel;
using MyWebProjectDAL;

namespace MyWebProjectBLL
{
    public class GradeManager
    {
        GradeServices gs = new GradeServices();
        public string GetGradeById(int Id)
        {
            return gs.GetGradeById(Id);
        }
        public Grade GetGradeAllById(int Id)
        {
            return gs.GetGradeAllById(Id);
        }
        public List GetGradeAll()
        {
            return gs.GetGradeAll();
        }
    }
}

1.7 GridViewバインド
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebProjectBLL;
using MyWebProjectModel;

namespace MyWebProject
{
    public partial class FromStudentAll : System.Web.UI.Page
    {
        StudentManager sm = new StudentManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetDataBind();
        }
        public void GetDataBind()
        {
            List list = sm.GetStudentAll();
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
}

1.8 DropDownListコントロールバインド

ここでは、エンティティクラスを りタイプとして すると に たないことに してください。


バインド は ではないが、 を する のバインド の で べている.
1.3 DAL.Studioサービスクラス