一:首先实现的是显示功能:
这次是控制显示,不能把一些敏感的信息显示出来
首先将自动显示更改为false
在学生列表上双击,即进入——load,记得我当时怎么也找不到——load,老师说带——的都是事件处理。
private void StudentListForm_Load(object sender, EventArgs e)
{
gride.AutoGenerateColumns = false;
}
要特别注意theclass这项,这是要和实体类相一致,而不是和数据库SQL server中的相一致
这是都将学生列表和添加学生的窗体设置为学生信息管理的子窗体
设置如下:
private void 添加学生ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddStudent f = new AddStudent();
f.MdiParent=this;
f.Show();
}
private void 学生列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
StudentListForm f = new StudentListForm();
f.MdiParent = this;
f.Show();
}
这时主窗体才起作用。且颜色发生明显变化
二:实现双击显示双击单元格的信息:
先上图看一下效果:
找到grid的双击处理事件,双击
代码如下:
private void gride_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string content = gride.CurrentCell.Value.ToString();
int row = gride.CurrentCell.RowIndex;
int col=gride.CurrentCell.ColumnIndex;
string s = string.Format("你双击的单元格:{0}行{1}列 值为{2}", row + 1, col + 1, content);
MessageBox.Show(s);
}
首先在数据访问层 中调用Dbhelper.cs中连接数据库的方法,实现如下操作
public void delete(string id)
{
string sql = string.Format("delete from student where id='{0}'", id);
DbHelper.myExecute(sql);
}
public void delete(string id)
{
StudentDal dal = new StudentDal();
dal.delete(id);
}
private void refreshDate(StudentBll bll)
{
List<Student> list = bll.getALl();
gride.DataSource = list;
}
private void button3_Click(object sender, EventArgs e)
{
string id = gride.CurrentRow.Cells[0].Value.ToString();
string name = gride.CurrentRow.Cells[1].Value.ToString();
DialogResult r = MessageBox.Show("确实要删除["+id+" "+name+"]吗?", "提示", MessageBoxButtons.YesNo);
if (r == DialogResult.Yes)
{
StudentBll bll = new StudentBll();
bll.delete(id);
refreshDate(bll);
}
}
添加和取消设置如下:
private void button1_Click(object sender, EventArgs e)
{
Student student = new Student();
student.Id = textId.Text;
student.Name = textname.Text;
student.Gender = radiomale.Checked ? "男" : "女";
student.Major = textmajor.Text;
student.Grade = int.Parse(textgrade.Text);
student.TheClass =int.Parse( textclass.Text);
StudentBll bll = new StudentBll();
bll.insert(student);
// MessageBox.Show("添加成功!");
this.DialogResult = DialogResult.OK;
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}