1. Realize the linkage of level two drop-down box.
Function: Implementation Click the Grade drop-down box to load the corresponding account drop-down box.
First step: Load the data in the Grade drop-down box first.
01. Write a method in the Gradedal layer (data access layer) to query all grade information.
/// <summary> ///get grade information from the database/// </summary> /// <returns>Collection of list: Grade number, Grade name</returns> PublicList<grade>Selectgradeinfo () {List<Grade> list =NewList<grade>(); stringsql ="SELECT * from Grade"; DataTable DT=sqlhelper.executedatatable (SQL); foreach(DataRow Iteminchdt. Rows) {//an item represents a row objectGrade Grade =NewGrade (); Grade. Gradeid= Convert.ToInt32 (item["Gradeid"]); Grade. Gradename=item["Gradename"]. ToString (); List. ADD (grade); } returnlist; }
02. The method of invoking the data access layer at the GRADEBLL layer (the Business logic layer) is returned to the UI layer for invocation.
Public classGRADEBLL {gradedal gd=NewGradedal (); /// <summary> ///get grade information from the database/// </summary> /// <returns>Collection of list: Grade number, Grade name</returns> PublicList<grade>Selectgradeinfo () {returnGD. Selectgradeinfo (); } }
03. At the UI layer (presentation layer) Call the GRADEBLL layer method, receive it with the list<grade> type, bind to the drop-down box, and implement the binding of the Grade drop-down box.
//load the Grade drop-down box method, make a call in the Load event Public voidLoadingcbograde () {//method of calling the BLL layer, receiving it with the list collectionlist<grade> list =GB. Selectgradeinfo (); //The binding displays the value. Cbograde. DisplayMember ="Gradename"; //binds hidden values. Cbograde. ValueMember ="Gradeid"; //Binding Data SourcesCbograde. DataSource =list; }
Step two: In the Grade drop-down box, the SelectedIndexChanged (event triggered when the property value changes) loads the corresponding account information for that grade by calling the BLL layer method.
01. Write a method on the Subjectdal layer to query the grade's hidden value (that is, the grade number) according to the selected grade, and return a collection of subject objects.
/// <summary> ///search for subjects under this grade based on the selected grade number/// </summary> /// <returns>collection of Account objects</returns> PublicList<subject> Selectsubjectinfos (intID) {//Query account information based on grade number stringsql ="Select Subjectid,subjectname from subject where [email protected]"; SqlParameter SP=NewSqlParameter ("@gradeid", id); DataTable DT=sqlhelper.executedatatable (SQL,SP); List<Subject> list =NewList<subject>(); foreach(DataRow Iteminchdt. Rows) {Subject Subject=NewSubject (); Subject. Subjectid= Convert.ToInt32 (item["Subjectid"]); Subject. Subjectname= item["Subjectname"]. ToString (); List. ADD (subject); } returnlist; }
02. The method of invoking the data access layer at the SUBJECTBLL layer is returned to the UI layer to invoke.
/// <summary> /// Search for subjects under this grade based on the selected grade number /// </summary> /// <returns> collection of Account objects </returns> Public List<subject> Selectsubjectinfos (int ID) { return SD. Selectsubjectinfos (ID); }
03. Call the SUBJECTBLL layer method in the SelectedIndexChanged (event triggered when the property value changes) in the Grade drop-down box
//Instantiate SUBJECTBLLSUBJECTBLL SB =NewSUBJECTBLL (); //Grade drop-down box property change value triggered event Private voidCbograde_selectedindexchanged (Objectsender, EventArgs e) { //Get Grade number intId=Convert.ToInt32 (Cbograde. SelectedValue); //a method called the BLL layer is received with a collection of subject typesList<subject> list=sb. Selectsubjectinfos (ID); //add an item all in the Account down box. List. Insert (0,NewSubject {subjectid=-1, subjectname="All" }); //empty drop-down box dataCbosubject. DataSource =NULL; //Binding Display ValuesCbosubject. DisplayMember ="Subjectname"; //binding hidden ValuesCbosubject. ValueMember ="Subjectid"; //Binding Data SourcesCbosubject. DataSource =list; }
Achieve level two drop-down box linkage