GridView的操作:基本操作(编辑、更新、取消)
GridView的基本操作:编辑 事件:RowEditing
1protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
2{
3GridView1.EditIndex = e.NewEditIndex;
4GridView_DataBind();//需要重新绑定
5}
GridView的基本操作:更新 事件:RowUpdating2{
3GridView1.EditIndex = e.NewEditIndex;
4GridView_DataBind();//需要重新绑定
5}
1protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
2{
3int click, score, times;
4string name;
5
6if (REG.IsMatch(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text))
7{
8click = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
9}
10else
11{
12click = int.Parse(GridView1.Rows[e.RowIndex].Cells[3].Text);
13}
14//源代码中GridView用TemplateField实现,但是也可以适用下属方法取得单元格的值
15//也可以用Row[e.RowIndex].FindControl("控件ID")获取控件,再进行类型转换
16score = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
17times = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
18name = GridView1.Rows[e.RowIndex].Cells[0].Text;
19//调用更新数据库的方法
20string message = Gp.UpdatePinlun(click, times, score, name);
21//关闭编辑模式
22GridView1.EditIndex = -1;
23//重新绑定数据
24GridView_DataBind();
25}
GridView的基本操作:取消更新 事件:RowCancelingEdit2{
3int click, score, times;
4string name;
5
6if (REG.IsMatch(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text))
7{
8click = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
9}
10else
11{
12click = int.Parse(GridView1.Rows[e.RowIndex].Cells[3].Text);
13}
14//源代码中GridView用TemplateField实现,但是也可以适用下属方法取得单元格的值
15//也可以用Row[e.RowIndex].FindControl("控件ID")获取控件,再进行类型转换
16score = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
17times = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
18name = GridView1.Rows[e.RowIndex].Cells[0].Text;
19//调用更新数据库的方法
20string message = Gp.UpdatePinlun(click, times, score, name);
21//关闭编辑模式
22GridView1.EditIndex = -1;
23//重新绑定数据
24GridView_DataBind();
25}
1protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
2{
3//关闭编辑模式
4GridView1.EditIndex = -1;
5//数据绑定
6GridView_DataBind();
7}
2{
3//关闭编辑模式
4GridView1.EditIndex = -1;
5//数据绑定
6GridView_DataBind();
7}