前言
学生基本信息维护,需要维护得就是学生得信息 - 如何维护 —当然是修改更新了。如何做到呢,下边跟随小编去看看吧~
正文
首先,我们知道,机房重构中得组合查询可以用存储过程和模板方法去实现,所以所有得代码写到父窗体即可,但是这个学生信息维护得修改click事件就得写到子窗体了。
首先增加一个按钮“修改”
1/在按钮的click事件里,将表格里得数据传给student实体。
2/实例化修改窗体,调用修改窗体得方法,将传给实体得纸传给修改窗体。
3/修改窗体显示
private void btnModify_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow == null)
{
MessageBox.Show("请先选中要修改的行");
return;
}
else
{
//通过实体将值传过去
Entity.Student_Info student = new Entity.Student_Info();
student.cardNo = Convert.ToInt32(dataGridView1.CurrentRow.Cells["cardNo"].Value.ToString());
student.Class = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Class"].Value.ToString());
student.department = dataGridView1.CurrentRow.Cells["department"].Value.ToString();
student.grade = Convert.ToInt32(dataGridView1.CurrentRow.Cells["grade"].Value.ToString());
student.sex = dataGridView1.CurrentRow.Cells["sex"].Value.ToString();
student.stuName = dataGridView1.CurrentRow.Cells["stuName"].Value.ToString();
student.stuNo = Convert.ToInt32(dataGridView1.CurrentRow.Cells["stuNo"].Value.ToString());
student.userID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["userID"].Value.ToString());
student.major = dataGridView1.CurrentRow.Cells["major"].Value.ToString();
student.explain = dataGridView1.CurrentRow.Cells["explain"].Value.ToString();
modifyinfomation modify = new modifyinfomation();
modify.getdata(student);
modify.ShowDialog();
设置修改窗体,窗体得DAL层得是更新语句
代码如下:
//传值方法(通过student实体传值)
public void getdata (Entity.Student_Info student)
{
txtCardno.Text = Convert.ToString(student.cardNo);
txtClass.Text = Convert.ToString(student.Class);
txtDe.Text = Convert.ToString(student.department);
txtGrade.Text = Convert.ToString(student.grade);
txtStuNo.Text =Convert.ToString( student.stuNo);
txtName.Text = student.stuName;
txtSex.Text = student.sex;
userID.Text = Convert.ToString(student.userID);
txtMajor.Text = student.major;
txtExplain.Text = student.explain;
}
private void btnModify_Click(object sender, EventArgs e)
{
Entity.Student_Info newstudent = new Entity.Student_Info();
Facade.modifyInformationFacade facade = new Facade.modifyInformationFacade();
newstudent.cardNo = Convert.ToInt32(txtCardno.Text);
newstudent.Class = Convert.ToInt32(txtClass.Text);
newstudent.department = txtDe.Text;
newstudent.grade = Convert.ToInt32(txtGrade.Text);
newstudent.sex =txtSex.Text;
newstudent.stuName = txtStuNo.Text;
newstudent.stuNo = Convert.ToInt32(txtStuNo.Text);
newstudent.explain = txtExplain.Text;
newstudent.major = txtMajor.Text;
DataTable table = facade.modifyinformation(newstudent);
MessageBox.Show("修改成功!");
后记
终于明白了传值,谢天谢地谢赵雷。