private void btnBuyMaterial_Click(object sender, EventArgs e)
        {
            if (dgvStudents.SelectedRows.Count <= 0)
                return;
            DataGridViewRow row = dgvStudents.SelectedRows[0];

            BuyMaterialsForm frmBuyMaterials = new BuyMaterialsForm();
            frmBuyMaterials.StudentName = txtName.Text;
            frmBuyMaterials.Balance = txtBalance.Text;
            if(frmBuyMaterials.ShowDialog() != DialogResult.OK)
                return;
            // 1> 添加购买记录
            materialsCostTableAdapter.Insert(int.Parse(txtID.Text), frmBuyMaterials.MaterialID,
                frmBuyMaterials.MaterialPrice, frmBuyMaterials.BuyCount, frmBuyMaterials.TotalCost,
                DateTime.Now, User.CurrentUser.UserName);
            // 2> 更新balance
            int balance = int.Parse(txtBalance.Text);
            balance -= frmBuyMaterials.TotalCost;
            studentsTableAdapter.UpdateBalance(balance, int.Parse(txtID.Text));
            txtBalance.Text = balance.ToString();
            row.Cells["Balance"].Value = balance;

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                List<PrintRecord> list = new List<PrintRecord>();
                PrintRecord prAmount = new PrintRecord();
                prAmount.id = 1;
                prAmount.name = frmBuyMaterials.MaterialName;
                prAmount.num = frmBuyMaterials.BuyCount+"";
                prAmount.price = frmBuyMaterials.MaterialPrice+"";
                prAmount.amount = frmBuyMaterials.TotalCost + "";
                list.Add(prAmount);

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = frmBuyMaterials.TotalCost;
                printForm.Show();
            }
        }
        private void btnExtendSignUp_Click(object sender, EventArgs e)
        {
            if (dgvStudentCourses.SelectedRows.Count <= 0)
                return;
            int rowIndex = dgvStudents.CurrentRow.Index;
            DataGridViewRow row = dgvStudentCourses.SelectedRows[0];

            // Get course id;
            int selectedStudentCourseIndex = GetSelectedStudentCourseIndex();
            int studentCourseId = studentsDataSet.StudentCourses.Rows[selectedStudentCourseIndex].Field<int>("ID");
            int courseID = studentsDataSet.StudentCourses.Rows[selectedStudentCourseIndex].Field<int>("CourseID");
            int studentId = int.Parse(txtID.Text);

            CourseExtendForm frmCourseExtend = new CourseExtendForm();
            frmCourseExtend.StudentName = txtName.Text;
            frmCourseExtend.Balance = txtBalance.Text;

            frmCourseExtend.CurrentExpireDate = row.Cells["ExpireTimeColumn"].Value.ToString();
            frmCourseExtend.CourseType = row.Cells["CourseTypeColumn"].Value.ToString();
            frmCourseExtend.CourseSubtype = row.Cells["CourseSubtypeColumn"].Value.ToString();
            frmCourseExtend.CourseName = row.Cells["CourseNameColumn"].Value.ToString();
            frmCourseExtend.ChargeType = row.Cells["ChargeTypeColumn"].Value.ToString();
            frmCourseExtend.ChargeAmount = row.Cells["ChargeAmountColumn"].Value.ToString();

            if (frmCourseExtend.ShowDialog() != DialogResult.OK)
                return;

            // 续报班:
            // 1> 扣除余额
            int balance = int.Parse(txtBalance.Text);
            balance -= frmCourseExtend.ActualCost;
            studentsTableAdapter.UpdateBalance(balance, int.Parse(txtID.Text));
            studentCoursesTableAdapter.UpdateBalance(frmCourseExtend.TotalCost, frmCourseExtend.signUpTime, studentId, courseID);
            txtBalance.Text = balance.ToString();

            // 2> 添加扣钱记录
            studentCostTableAdapter1.InsertCostWithExpireTime(int.Parse(txtID.Text), courseID, frmCourseExtend.TotalCost, DateTime.Now, 100,
                frmCourseExtend.ActualCost, frmCourseExtend.DiscountReason, User.CurrentUser.UserName, frmCourseExtend.ExpireDate);

            // 3> 更新课程到期时间
            studentCoursesTableAdapter.UpdateExpireTime(frmCourseExtend.ExpireDate, studentCourseId);

            // 4> 更新学生的学费过期时间,看哪个过期时间更近
            studentsTableAdapter.UpdateExpireTime(frmCourseExtend.ExpireDate, int.Parse(txtID.Text));

            // 5> 刷新显示
            studentCoursesTableAdapter.Fill(studentsDataSet.StudentCourses, studentId);
            if (this.UserType == 0)
            {
                // 只加载被授权的学生
                this.studentsTableAdapter.FillByUserName(this.studentsDataSet.Students, this.UserName);
            }
            else
            {
                // 加载所有的学生
                this.studentsTableAdapter.FillByStatus(this.studentsDataSet.Students);
            }
            if (rowIndex >= 0)
            {
                this.dgvStudents.Rows[rowIndex].Selected = true;
                this.dgvStudents.CurrentCell = this.dgvStudents.Rows[rowIndex].Cells[0];
            }

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                List<PrintRecord> list = new List<PrintRecord>();
                PrintRecord prAmount = new PrintRecord();
                prAmount.id = 1;
                prAmount.name = frmCourseExtend.CourseName + "学费";
                prAmount.num = "1";
                prAmount.price = frmCourseExtend.ActualCost.ToString();
                prAmount.amount = frmCourseExtend.ActualCost.ToString();
                prAmount.remark = "续报";
                list.Add(prAmount);

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = frmCourseExtend.ActualCost;
                printForm.Show();
            }
        }
        private void btnCharge_Click(object sender, EventArgs e)
        {
            if (dgvStudents.SelectedRows.Count <= 0)
                return;

            int studentId = int.Parse(txtID.Text);

            GetDepositAmountForm frmGetDepositAmount = new GetDepositAmountForm();
            frmGetDepositAmount.StudentName = txtName.Text;
            frmGetDepositAmount.CardNo = txtCardNo.Text;
            frmGetDepositAmount.CardType = txtCardType.Text;
            if (frmGetDepositAmount.ShowDialog() != DialogResult.OK)
                return;

            int depositAmount = frmGetDepositAmount.DepositAmount;
            int paidAmount = frmGetDepositAmount.PaidAmount;
            int tuitionAmount = frmGetDepositAmount.tuitionAmount;
            int materialsAmount = frmGetDepositAmount.materialsAmount;
            int otherAmount = frmGetDepositAmount.otherAmount;

            DataGridViewRow row = dgvStudents.SelectedRows[0];

            ConfirmDepositForm frmConfirmDeposit = new ConfirmDepositForm();
            frmConfirmDeposit.DepositAmount = depositAmount.ToString();
            frmConfirmDeposit.PaidAmount = paidAmount.ToString();
            frmConfirmDeposit.TuitionAmount = tuitionAmount.ToString();
            frmConfirmDeposit.MaterialsAmount = materialsAmount.ToString();
            frmConfirmDeposit.OtherAmount = otherAmount.ToString();
            frmConfirmDeposit.StudentName = txtName.Text;
            frmConfirmDeposit.StudentSex = row.Cells["SexColumn"].Value.ToString();
            frmConfirmDeposit.StudentBirthday = row.Cells["BirthdayColumn"].Value.ToString();
            frmConfirmDeposit.StudentAddress = row.Cells["AddressColumn"].Value.ToString();
            frmConfirmDeposit.StudentPhone = row.Cells["PhoneColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherName = row.Cells["FartherNameColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherPhone = row.Cells["FartherTelColumn"].Value.ToString();
            frmConfirmDeposit.StudentFartherWork = row.Cells["FartherWorkColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherName = row.Cells["MotherNameColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherPhone = row.Cells["MotherTelColumn"].Value.ToString();
            frmConfirmDeposit.StudentMotherWork = row.Cells["MotherWorkColumn"].Value.ToString();
            frmConfirmDeposit.Text = "确定为“" + txtName.Text + "”充值" + depositAmount + "元吗?";
            if (frmConfirmDeposit.ShowDialog() != DialogResult.OK)
                return;

            depositListTableAdapter.Insert(studentId, depositAmount, DateTime.Now, paidAmount, User.CurrentUser.UserName);

            int newBalance = int.Parse(txtBalance.Text) + depositAmount;
            studentsTableAdapter.UpdateBalance(newBalance, studentId);

            txtBalance.Text = newBalance.ToString();
            row.Cells["Balance"].Value = txtBalance.Text;

            if (MessageBox.Show("需要打印发票吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
                List<PrintRecord> list = new List<PrintRecord>();
                if (tuitionAmount == 0 && materialsAmount == 0 && otherAmount == 0)
                {
                    PrintRecord prAmount = new PrintRecord();
                    prAmount.id = 1;
                    prAmount.name = "充值";
                    prAmount.num = "1";
                    prAmount.price = paidAmount.ToString();
                    prAmount.amount = paidAmount.ToString();
                    list.Add(prAmount);
                }
                else {
                    if (tuitionAmount != 0)
                    {
                        PrintRecord prTuition = new PrintRecord();
                        prTuition.id = 1;
                        prTuition.name = "学费";
                        prTuition.num = "1";
                        prTuition.price = tuitionAmount.ToString();
                        prTuition.amount = tuitionAmount.ToString();
                        list.Add(prTuition);
                    }

                    if (materialsAmount != 0)
                    {
                        PrintRecord prMaterials = new PrintRecord();
                        prMaterials.id = 2;
                        prMaterials.name = "材料费";
                        prMaterials.num = "1";
                        prMaterials.price = materialsAmount.ToString();
                        prMaterials.amount = materialsAmount.ToString();
                        list.Add(prMaterials);
                    }

                    if (otherAmount != 0)
                    {
                        PrintRecord prOther = new PrintRecord();
                        prOther.id = 3;
                        prOther.name = "其他";
                        prOther.num = "1";
                        prOther.price = otherAmount.ToString();
                        prOther.amount = otherAmount.ToString();
                        list.Add(prOther);
                    }
                }

                InvoicePrintingForm printForm = new InvoicePrintingForm();
                printForm.MdiParent = this.Owner;
                printForm.StartPosition = FormStartPosition.CenterScreen;
                printForm.isReadOnly = true;
                printForm.userName = txtName.Text;
                printForm.recordList = list;
                printForm.totalAmount = depositAmount;
                printForm.Show();
            }
        }