private void dgvInRec_CellContentClick(object sender, DataGridViewCellEventArgs e) { //如果双击的记录,则显示详细信息 if (e.RowIndex >= 0) { //获取要显示详细信息的记录 IncomeRecord rec; rec = (IncomeRecord)this.dgvInRec.Rows[e.RowIndex].Tag; //创建只读的记录查看对话框 IncomeReForm dlg; dlg = new IncomeReForm(this.curUser.Name, rec, true); //显示对话框 dlg.ShowDialog(this); } }
private void btnAddNewIncomRec_Click(object sender, EventArgs e) { //创建用来保存新记录的对象 int newId = IncomeSQL.GetUsableIncomID(this.curUser.Name); if (newId == 0) { MessageBox.Show(this, "无法获取可用的收入记录编号,请确保数据库连接正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } IncomeRecord rec = new IncomeRecord(); rec.ID = newId; rec.IncomeTime = DateTime.Now; rec.RecordTime = DateTime.Now; //创建收入记录编辑对话框 IncomeReForm dlg = new IncomeReForm(this.curUser.Name, rec, false); if (dlg.ShowDialog(this) == DialogResult.OK) { //添加记录成功,则添加到界面 if (IncomeSQL.AddIncomRec(this.curUser.Name, rec)) { //添加一行到界面 int newRowIndex = this.dgvInRec.Rows.Add(); this.dgvInRec.Rows[newRowIndex].Tag = rec; //显示信息到新添加的行 this.dgvInRec.Rows[newRowIndex].Cells["colInID"].Value = rec.ID; this.dgvInRec.Rows[newRowIndex].Cells["colInInTime"].Value = rec.IncomeTime; this.dgvInRec.Rows[newRowIndex].Cells["colInType"].Value = rec.IncomeType; this.dgvInRec.Rows[newRowIndex].Cells["colInUsage"].Value = rec.IncomUsage; this.dgvInRec.Rows[newRowIndex].Cells["colInRecTime"].Value = rec.RecordTime; this.dgvInRec.Rows[newRowIndex].Cells["colInDes"].Value = rec.Description; this.dgvInRec.Rows[newRowIndex].Cells["colInBank"].Value = rec.BankCard; this.dgvInRec.Rows[newRowIndex].Cells["colInAmount"].Value = rec.Amount; } else { MessageBox.Show(this, "添加记录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
private void btnModifyIncomRec_Click(object sender, EventArgs e) { //判断是否有选中的记录 if (this.dgvInRec.SelectedRows.Count == 0) { MessageBox.Show(this, "没有选中任何记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //获取选中记录的数据 IncomeRecord rec; rec = (IncomeRecord)this.dgvInRec.SelectedRows[0].Tag; //创建可修改对话框 IncomeReForm dlg = new IncomeReForm(this.curUser.Name, rec, false); //显示对话框,进行编辑操作 if (dlg.ShowDialog(this) == DialogResult.OK) { //修改记录成功,则更新指定记录 if (IncomeSQL.ModifyIncomrec(this.curUser.Name, rec)) { this.dgvInRec.SelectedRows[0].Cells["colInID"].Value = rec.ID; this.dgvInRec.SelectedRows[0].Cells["colInInTime"].Value = rec.IncomeTime; this.dgvInRec.SelectedRows[0].Cells["colInType"].Value = rec.IncomeType; this.dgvInRec.SelectedRows[0].Cells["colInUsage"].Value = rec.IncomUsage; this.dgvInRec.SelectedRows[0].Cells["colInRecTime"].Value = rec.RecordTime; this.dgvInRec.SelectedRows[0].Cells["colInDes"].Value = rec.Description; this.dgvInRec.SelectedRows[0].Cells["colInBank"].Value = rec.BankCard; this.dgvInRec.SelectedRows[0].Cells["colInAmount"].Value = rec.Amount; } else { MessageBox.Show(this, "修改记录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }