GridView编辑(更新和取消)全部代码···更新之后要有提示
展开全部
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
StudentDLL sdl = new StudentDLL();
sdl.Stu_id = Convert.ToInt32(GridView1.DataKeys[0].Value);
sdl.Stu_name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
sdl.Stu_sex = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("ddlSex")).SelectedValue.ToString();
sdl.Stu_age =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
sdl.Class_id = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("ddlClass")).SelectedValue);
int efectRows = (new StudentBLL()).updateStudent(sdl);
if (efectRows > 0)
{
GridView1.EditIndex = -1;
BindGrid();
Page.RegisterStartupScript("","<script>alert('更新成功!')</script>");
}
else
{
Page.RegisterStartupScript("","<script>alert('更新失败!')</script>");
}
}
以上性别和班级是用 的下拉框
下面是取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
这两个事件在gridview的事件中找到双击。然后在出来的方法中填入响应代码即可!
{
StudentDLL sdl = new StudentDLL();
sdl.Stu_id = Convert.ToInt32(GridView1.DataKeys[0].Value);
sdl.Stu_name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
sdl.Stu_sex = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("ddlSex")).SelectedValue.ToString();
sdl.Stu_age =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
sdl.Class_id = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("ddlClass")).SelectedValue);
int efectRows = (new StudentBLL()).updateStudent(sdl);
if (efectRows > 0)
{
GridView1.EditIndex = -1;
BindGrid();
Page.RegisterStartupScript("","<script>alert('更新成功!')</script>");
}
else
{
Page.RegisterStartupScript("","<script>alert('更新失败!')</script>");
}
}
以上性别和班级是用 的下拉框
下面是取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
这两个事件在gridview的事件中找到双击。然后在出来的方法中填入响应代码即可!
追问
我用三层架构Subsonic做的··人家不让用着方法啊···那该怎么写呢?
追答
哦。orm的话那也得在gridview的那两个事件中写的吧我觉得。我没有用过subsonic,所以...不过你可以尝试在那两个事件中找到subsonic中的相应的更新方法。试着去做。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
{
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();
}
追问
这里的字段1 2 3 分别代表的是什么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询