GridView编辑(更新和取消)全部代码···更新之后要有提示
答案:2 悬赏:10 手机版
解决时间 2021-02-04 19:40
- 提问者网友:辞取
- 2021-02-03 21:18
GridView编辑(更新和取消)全部代码···更新之后要有提示
最佳答案
- 五星知识达人网友:逐風
- 2021-02-03 21:29
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)<br> {<br> StudentDLL sdl = new StudentDLL();<br> sdl.Stu_id = Convert.ToInt32(GridView1.DataKeys[0].Value);<br> sdl.Stu_name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;<br> sdl.Stu_sex = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("ddlSex")).SelectedValue.ToString();<br> sdl.Stu_age =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);<br> sdl.Class_id = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("ddlClass")).SelectedValue);<br> int efectRows = (new StudentBLL()).updateStudent(sdl);<br> if (efectRows > 0)<br> {<br> GridView1.EditIndex = -1;<br> BindGrid();<br> Page.RegisterStartupScript("","<script>alert('更新成功!')</script>");<br> }<br> else<br> {<br> Page.RegisterStartupScript("","<script>alert('更新失败!')</script>");<br> }<br> }<br>以上性别和班级是用 的下拉框<br>下面是取消<br> protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)<br> {<br> GridView1.EditIndex = -1;<br> BindGrid();<br> }<br><br>这两个事件在gridview的事件中找到双击。然后在出来的方法中填入响应代码即可!
全部回答
- 1楼网友:大漠
- 2021-02-03 21:36
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯