C# student management system - class list (query function)

C# student management system - class list of class management (query function)

The last grade list is ready. Today we will make the class list in class management. Different from the grade list, the class list has a query function, as shown below:Insert image description here

Interface layout part:

Simply write down, query that this light-colored box is a GroupBox, and its function is a box with a title (just for aesthetics, not useful). These are Label labels for grades. Combobox is used to select the drop-down menu box for grades, and class textbox , query button button.
The second half is to display the appearance of the table, which is the same as the interface setting of the grade list last time, not much to write, control Datagridview.

Name the several controls to be used:
the drop-down menu of the grade -
the textbox of the cmbgrade class -
the table for displaying the information in the lower half of the textclass Datagridview - dgvclass

Database:
The database is named class and has 4 key information, as shown in the figure below:
cid - class number,
cname - class name,
gid - grade number (to be connected with the grade table later),
remark - remarks.
Insert image description here

Function implementation part:

First, when we click on the class list interface, the drop-down menu of the class and the total class list will be automatically loaded, and two initialization functions will be called:

private void list_class_Load(object sender, EventArgs e)
        {
    
    
         
            initgrade();   //初始化加载年级列表
            initclass();   //初始化加载班级列表
        }

Initializing the grade list:
The general idea is: the drop-down menu needs Datatable to obtain, and is assigned to cmbclass
1. Write a sql statement to obtain grade information, and Datatable to obtain the sql statement:

 string sql = "select * from [grade]";
 DataTable dtgrade = sqlhelper.getDataTable(sql);

2. Get the data in the grade table

 //获取grade表中数据
            cmbgrade.DataSource = dtgrade;        //comboBox来获取datatable 
            cmbgrade.DisplayMember = "gname";       //设置绑定的数据库对应的名称,显示的内容
            cmbgrade.ValueMember = "gid";

3. (Additional step) By default it shows "Please select";

 //默认“请选择“;添加一行
            DataRow drow = dtgrade.NewRow();
            drow["gid"] = 0;
            drow["gname"] = "请选择";
            dtgrade.Rows.InsertAt(drow, 0); //使dt.Rows.Add(dr)只是把选项追加到最后,而使用dt.Rows.InsertAt(dr, 0)可以把增加的选项插入到相应的位置上

Initializing the class list:
The initial load here needs to display the number, class, and grade information. The gid of the class table in the database needs to match the gid of the grade table to display the grade name. The database uses inner join...on to associate the two tables. .

			string sql = "SELECT cid, cname, gname FROM class c INNER JOIN grade g ON c.gid = g.gid ORDER BY c.cid";
            DataTable dclass = sqlhelper.getDataTable(sql);
            dgvclass.DataSource = dclass;

After the initialization is complete, only the query function remains, and then write a complex query button:

Query button function design:

1. Obtain user input information.
Pay attention here! ! ! Using SelectedValue to get the data in the drop-down menu box is of object type. You can try forced conversion, but my VS does not recognize it and always reports an error. After a lot of effort, I found that I can use ToString to get the string format, and finally convert it to int type. I really can't think of any other way. If anyone sees it and has a good idea, please private message me.

int setid=(int)cmbgrade.SelectedValue

  string _value = cmbgrade.SelectedValue.ToString();
  int setgid = int.Parse(_value);
  string setclass = textclass.Text.Trim();

2. Determine whether the obtained data is empty

Regardless of whether it is empty or not, we have to use the sql statement to initialize the class. Because the displayed content is the same, we add a where 1=1 after the previous sql statement to indicate that the sql statement is always true and determine whether the obtained setid is 0.
No. If it is 0, then add a condition to the sql statement. The entered grade ID matches the grade ID in the table. Similarly, it is judged whether the obtained setclass is empty and a fuzzy query is performed.

 string sql = "select c.cid, c.cname, g.gname from (class  c INNER JOIN grade g ON c.gid = g.gid) where 1=1 ";
            //判断
            if (setgid != 0)
            {
    
    
                sql += " and c.gid=@gid";
                
            }
            if (!string.IsNullOrEmpty(setclass))
            {
    
    
                sql += " and cname like @cname";
            }
            //定义参数数组 进行传参

            OleDbParameter[] paras ={
    
    
                                     new OleDbParameter("@gid",setgid),
                                     new OleDbParameter("@cname","%"+setclass+"%")
                                 };

            DataTable dclass = sqlhelper.getDataTable(sql,paras);
            dgvclass.DataSource = dclass;

Pay attention to the problem of fuzzy query here: do not add % in the SQL statement, and finally add % when passing parameters to be effective.
This part is finished here, run:
Insert image description herethe student list is similar to the class list, next time I will only write a little experience, and I will not write more about the layout and function realization.
Attached: Today’s complete code:

   private void list_class_Load(object sender, EventArgs e)
        {
    
    
         
            initgrade();   //初始化加载年级列表
            initclass();   //初始化加载班级列表
        }



        private void initgrade()
        {
    
    

            //下拉菜单也要datatable来获取,然后指定给comboBox
            //用displaymember来show显示的值
            string sql = "select * from [grade]";
            DataTable dtgrade = sqlhelper.getDataTable(sql);

            //默认“请选择“;添加一行
            DataRow drow = dtgrade.NewRow();
            drow["gid"] = 0;
            drow["gname"] = "请选择";
            dtgrade.Rows.InsertAt(drow, 0); //使用dt.Rows.Add(dr)只是把选项追加到最后,而使用dt.Rows.InsertAt(dr, 0)可以把增加的选项插入到相应的位置上

           //获取grade表中数据
            cmbgrade.DataSource = dtgrade;        //comboBox来获取datatable 
            cmbgrade.DisplayMember = "gname";       //设置绑定的数据库对应的名称,显示的内容
            cmbgrade.ValueMember = "gid";


        }

        private void initclass()
        {
    
    
            string sql = "SELECT cid, cname, gname FROM class c INNER JOIN grade g ON c.gid = g.gid ORDER BY c.cid";
            DataTable dclass = sqlhelper.getDataTable(sql);
            dgvclass.DataSource = dclass;
        }


        //查询
        private void btncha_Click(object sender, EventArgs e)
        {
    
    
           
            //获取输入信息
            string _value = cmbgrade.SelectedValue.ToString();
            int setgid = int.Parse(_value);

            string setclass = textclass.Text.Trim();

            string sql = "select c.cid, c.cname, g.gname from (class  c INNER JOIN grade g ON c.gid = g.gid) where 1=1 ";
            //判断
            if (setgid != 0)
            {
    
    
                sql += " and c.gid=@gid";
                
            }
            if (!string.IsNullOrEmpty(setclass))
            {
    
    
                sql += " and cname like @cname";
            }
            //定义参数数组 进行传参

            OleDbParameter[] paras ={
    
    
                                     new OleDbParameter("@gid",setgid),
                                     new OleDbParameter("@cname","%"+setclass+"%")
                                 };

            DataTable dclass = sqlhelper.getDataTable(sql,paras);
            dgvclass.DataSource = dclass;
        }

Guess you like

Origin blog.csdn.net/qq_42740834/article/details/105794180