Esempio n. 1
0
        private void btnSearchStudent_Click(object sender, EventArgs e)
        {
            //Create a list of students to store result(s) in
            List <Student> studentSearchResult = new List <Student>();

            //Date of birth is empty
            String DOB = "";

            //Initially set id to 0 (nothing entered in textbox)
            int id = 0;

            //If ID text box has something in it, replace 0 with the ID input by user
            if (txtID.Text != "")
            {
                if (txtID.MaskCompleted)
                {
                    id = Int32.Parse(txtID.Text);
                }
                else
                {
                    MessageBox.Show("Please enter a valid Student ID", "Invalid Student ID", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            //If the date time picker is not empty, put the value in the variable
            if (dtpDOB.Text != " ")
            {
                DOB = dtpDOB.Text;
            }


            //All of user input into variables
            String name   = txtName.Text;
            String gender = cmbGender.Text;
            String dept   = cmbDepartment.Text;

            //Create a new student with properties set
            Student searchStudent = new Student(id, name, gender, DOB, dept, "");

            //Create a connection to the database and search for the student
            LibraryDB student = new LibraryDB();

            //Search for the student
            studentSearchResult = student.StudentSearch(searchStudent);

            //Load the student data into the data grid view
            dgvStudent.DataSource = studentSearchResult;

            //Displays number of returned records to the user
            if (studentSearchResult.Count == 1)
            {
                lblNumRecords.Text = studentSearchResult.Count + " Student matches your filter.";
            }
            else
            {
                lblNumRecords.Text = studentSearchResult.Count + " Students match your filter.";
            }

            //Column headers
            dgvStudent.Columns[0].HeaderText = "Student ID";
            dgvStudent.Columns[1].HeaderText = "Student Name";
            dgvStudent.Columns[2].HeaderText = "Gender";
            dgvStudent.Columns[3].HeaderText = "Date of Birth";
            dgvStudent.Columns[4].HeaderText = "Department";
            dgvStudent.Columns[5].HeaderText = "Contact Number";

            //Dynamically size the data grid view each time the user searches
            int width = 0;

            foreach (DataGridViewColumn col in dgvStudent.Columns)
            {
                width += col.Width;
            }
            width += dgvStudent.RowHeadersWidth;

            dgvStudent.ClientSize      = new Size(width + 2, dgvStudent.Height);
            dgvStudent.BackgroundColor = System.Drawing.SystemColors.Control;
        }