Thứ Ba, 18 tháng 2, 2014

Coding trong form frmStudent đã có comment hướng dẫn hiểu chi tiết

Thiết kế form có hình dạng. Chú ý từ các đoạn code dưới, suy ra cách đặt tên:

Nội dung thiết kế bảng

Nội dung coding

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic; //Sử dụng các giá trị thiết lập ở lớp 2 đã tạo

namespace _3Layer
{
    public partial class frmStudent : Form
    {
        public frmStudent()
        {
            InitializeComponent();
        }

        Student StudentItem = new Student(); //biến toàn cụ StudentItem được khởi tạo
        string id1;        //biến toàn cục id1 được tạo để thực hiện truy vấn cập nhật và xóa
        Lop loph = new Lop(); //Định nghĩa một loph để thao tác trực tiếp trên Layer3
        int soluong;//Biến chứa giá trị số dòng dữ liệu có trong datagridview
        int dong; //Biến chứa giá trị index trong datagridview
       
        //Thực hiện các công việc khi form được mở ra
        private void frmStudent_Load(object sender, EventArgs e)
        {
            //Ô chứa dữ liệu tìm kiếm nếu có chứa chữ All thì hiện tất cả các thông tin có trong bảng
            //dữ liệu ra lưới. Nếu không thì hiện dữ liệu tìm kiếm có tên lớp chứa trong ô tìm kiếm
            DataTable dt = new DataTable();
            if (this.txtTK.Text == "All")
                dt = StudentItem.ShowStudent();
            else
                dt = StudentItem.lookStudent1(this.txtTK.Text);

            dt = StudentItem.ShowStudent(); //gọi hàm hiển thị sinh viên vào lưới đã viết trong lớp 2
            dtgStudent.DataSource = dt; //nguồn của lưới là bảng dữ liệu đã lấy ở trên
            this.label3.Text = "Có tổng số " + dtgStudent.RowCount + " Sinh viên";

            //Lấy dữ liệu cho điều khiển combobox
            //Biến bảng dữ liệu dt1 chứa dữ liệu mã lớp lấy trong bảng dữ liệu tblClass
            //dữ liệu dt1 sau đó được gán nguồn cho nút combo
            //Trên form có điều khiển lấy ngày tháng, cần định dạng lại ngày tháng cho phù hợp với
            //thói quen dùng ngày tháng của Việt nam
            DataTable dt1 = new DataTable();
            dt1 = loph.GetClassID();
            cbofldClassID.DataSource = dt1;
            cbofldClassID.DisplayMember = "fldID";
            txtfldCreateDate.Format = DateTimePickerFormat.Custom;
            txtfldCreateDate.CustomFormat = "dd/MM/yyyy";
        }

        //Hàm làm số thứ tự cho gridview
        //Trong lưới tạo thêm ô đầu tiên là ô chứa stt bản ghi.
        //Dùng vòng for để khởi tạo giá trị cho các ô này
        private void dtgStudent_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            for (int i = 0; i < dtgStudent.RowCount; i++)
                dtgStudent.Rows[i].Cells[0].Value = i + 1;
        }

        //Hàm cho nút reset, nút này xóa tất cả các ô textbox và thiết lập các giá trị mặc định
        //cho các textbox này để nhập dữ liệu được nhanh chóng.
        //Chú ý mã sinh viên được khởi tạo tự động bằng cách
        //ghép mã lớp chứa trong ô tìm kiếm và số thứ tự bản ghi trong lưới +1
        //vì muốn thể hiện số thứ tự theo dạng xxx nên nếu stt có 2 chữ số thì sẽ tự động thêm
        //số 0 vào trước số thứ tự
        private void btnReset_Click(object sender, EventArgs e)
        {
            DateTime tg = new DateTime();//Định nghĩa đối tượng tg để lấy giá trị ngày tháng
                                          //tháng mặc định của hệ thống
            soluong++;
            if (soluong.ToString().Length<3 p="">                this.txtfldID.Text = this.txtTK.Text + "0" + soluong.ToString();
            else
                this.txtfldID.Text = this.txtTK.Text + soluong.ToString();
            this.txtfldFirstName.Clear();
            this.txtfldLastName.Clear();
            this.txtfldAge.Clear();
            this.txtfldSex.Value = 0;
            this.txtfldCreateDate.Text = tg.Date.Day.ToString() + "/" + tg.Date.Month.ToString() + "/" + tg.Date.Year.ToString();
            this.txtfldID.Focus();
        }
           
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        //Buộc dữ liệu từ DataGridView ra ô textBox
        private void dtgStudent_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;//Lấy chỉ số dòng của DataGridView
            //Các Cell của dòng đánh số từ 0 đến hết. Chú ý khi thiết kế DGV số cột
            id1 = dtgStudent.Rows[dong].Cells[0].Value.ToString();
            this.txtfldID.Text = dtgStudent.Rows[dong].Cells[0].Value.ToString().Trim(); ;
            this.txtfldFirstName.Text = dtgStudent.Rows[dong].Cells[1].Value.ToString().Trim();
            this.txtfldLastName.Text = dtgStudent.Rows[dong].Cells[2].Value.ToString().Trim();
            this.txtfldAge.Text = dtgStudent.Rows[dong].Cells[3].Value.ToString();
            this.txtfldSex.Text = dtgStudent.Rows[dong].Cells[4].Value.ToString().Trim();
            this.txtfldCreateDate.Text = dtgStudent.Rows[dong].Cells[5].Value.ToString().Trim();
            this.cbofldClassID.Text = dtgStudent.Rows[dong].Cells[6].Value.ToString().Trim();
        }

        //Nút thêm dữ liệu sinh viên, ghi vào CSDL
        private void btnThem_Click(object sender, EventArgs e)
        {
            this.txtfldID.Text = this.txtfldID.Text.Trim();
            if (this.txtfldID.TextLength == 0)
                MessageBox.Show("Mã SV không được để trống");
            else
                if (this.txtfldID.TextLength > 10)
                    MessageBox.Show("Mã SV không được vượt quá 10 ký tự");
                else
                    if (this.txtfldFirstName.TextLength == 0)
                        MessageBox.Show("Tên SV không được để trống");
                    else
                    {  //Hàm try để thực hiện một công việc
                        try
                        {
                            StudentItem.InsertStudent(this.txtfldID.Text, this.txtfldFirstName.Text, this.txtfldLastName.Text, this.txtfldAge.Text, this.txtfldSex.Text, this.txtfldCreateDate.Text, this.cbofldClassID.Text);
                            DataTable dt = new DataTable();
                            dt = StudentItem.lookStudent1(this.txtTK.Text);
                            dtgStudent.DataSource = dt;
                            soluong = dtgStudent.RowCount;
                            this.label3.Text = "Có tổng số " + soluong.ToString() + " sinh viên";
                        }
                        catch //Hàm catch để hiển thị khi thực hiện công việc gây lỗi
                        {
                            MessageBox.Show("Mã SV " + this.txtfldID.Text + " đã tồn tại");
                        }
                    }
        }

        //Nút sửa dữ liệu, sửa xong ghi vào CSDL
        private void btnSua_Click(object sender, EventArgs e)
        {
            this.txtfldID.Text = this.txtfldID.Text.Trim();
            if (this.txtfldID.TextLength == 0)
                MessageBox.Show("Bạn cần phải chọn Mã để sửa");
            else
                if (this.txtfldID.TextLength > 10)
                    MessageBox.Show("Mã không được vượt quá 10 ký tự");
                else
                    if (this.txtfldFirstName.TextLength == 0)
                        MessageBox.Show("Tên không được để trống");
                    else
                    {
                        StudentItem.UpdateStudent(id1, this.txtfldID.Text, this.txtfldFirstName.Text, this.txtfldLastName.Text, this.txtfldAge.Text, this.txtfldSex.Text, this.txtfldCreateDate.Text, this.cbofldClassID.Text);
                        DataTable dt = new DataTable();
                        dt = StudentItem.lookStudent1(this.txtTK.Text);
                        dtgStudent.DataSource = dt;
                        soluong = dtgStudent.RowCount;
                        this.label3.Text = "Có tổng số " + soluong.ToString() + " sinh viên";

                    }
        }

        //Nút xóa dữ liệu sinh viên
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (this.txtfldID.TextLength == 0)
                MessageBox.Show("Bạn cần chọn lớp để xóa");
            else
            {
                if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn xóa không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    StudentItem.DeleteStudent(id1);
                    MessageBox.Show("Đã xóa thành công");
                    DataTable dt = new DataTable();
                    dt = StudentItem.lookStudent1(this.txtTK.Text);
                    dtgStudent.DataSource = dt;
                    soluong = dtgStudent.RowCount;
                    this.label3.Text = "Có tổng số " + soluong + " sinh viên";
                }
            }
        }

        //Nút tìm kiếm theo tên, họ đệm của sinh viên
        private void btnTK_Click(object sender, EventArgs e)
        {
            if (this.txtTK.MaxLength == 0)
                MessageBox.Show("Bạn chưa nhập từ khóa tìm kiếm");
            else
            {
                DataTable dt = new DataTable();
                dt = StudentItem.lookStudent(this.txtTK.Text);
                dtgStudent.DataSource = dt;
                soluong = dtgStudent.RowCount;
                this.label3.Text = "Có tổng số " + soluong.ToString() + " sinh viên";
                if (dt.Rows.Count == 0)
                {
                    this.lblStatus.ForeColor = Color.Red;
                    this.lblStatus.Text = "Không tìm thấy dữ liệu";
                }
                else
                    this.lblStatus.ResetText();
            }
        }

        //Nút hủy kết quả tìm kiếm
        private void btnTroVe_Click(object sender, EventArgs e)
        {
            frmStudent_Load(sender, e);
            this.lblStatus.ResetText();
        }

        //Nút tìm kiếm theo lớp
        private void btnTimLop_Click(object sender, EventArgs e)
        {

            if (this.txtTK.MaxLength == 0)
                MessageBox.Show("Bạn chưa nhập từ khóa tìm kiếm");
            else
            {
                DataTable dt = new DataTable();
                if (this.txtTK.Text=="All")
                    dt = StudentItem.ShowStudent();
                else
                    dt = StudentItem.lookStudent1(this.txtTK.Text);
                dtgStudent.DataSource = dt;
                soluong = dtgStudent.RowCount;
                this.label3.Text = "Có tổng số " + soluong.ToString() + " sinh viên";
                if (dt.Rows.Count == 0)
                {
                    this.lblStatus.ForeColor = Color.Red;
                    this.lblStatus.Text = "Không tìm thấy dữ liệu";
                }
                else
                    this.lblStatus.ResetText();
            }

        }

        //Nút dịch chuyển sang phải
        private void button3_Click(object sender, EventArgs e)
        {
            dong++;
            soluong = dtgStudent.RowCount;
            if (dong>=soluong)
            {
                dong = 0;
            }
            hiendulieu();
        }

        public void hiendulieu()
        {
            this.txtfldID.Text = dtgStudent.Rows[dong].Cells[0].Value.ToString().Trim(); ;
            this.txtfldFirstName.Text = dtgStudent.Rows[dong].Cells[1].Value.ToString().Trim();
            this.txtfldLastName.Text = dtgStudent.Rows[dong].Cells[2].Value.ToString().Trim();
            this.txtfldAge.Text = dtgStudent.Rows[dong].Cells[3].Value.ToString();
            this.txtfldSex.Text = dtgStudent.Rows[dong].Cells[4].Value.ToString().Trim();
            this.txtfldCreateDate.Text = dtgStudent.Rows[dong].Cells[5].Value.ToString().Trim();
            this.cbofldClassID.Text = dtgStudent.Rows[dong].Cells[6].Value.ToString().Trim();
        }

        //Nút dịch chuyển sang trái
        private void button2_Click(object sender, EventArgs e)
        {
            dong= dong-1;
            soluong = dtgStudent.RowCount;
            if (dong <= 0)
            {
                dong = soluong-1;
            }
            hiendulieu();
        }

        //Nút về đầu danh sách
        private void button1_Click(object sender, EventArgs e)
        {
            dong = 0;
            soluong = dtgStudent.RowCount;
            hiendulieu();
        }

        //Nút về cuối danh sách
        private void button4_Click(object sender, EventArgs e)
        {
            dong = soluong - 1;
            soluong = dtgStudent.RowCount;
            hiendulieu();
        }
    }
}


Không có nhận xét nào:

Đăng nhận xét