RowEditing事件如何编写

我用DataTable表绑定了GridView但是我想可以编辑每一行的数据,我应该怎么做~中间没有用到数据库希望有具体解释的回复... 我用DataTable表绑定了GridView但是我想可以编辑每一行的数据,我应该怎么做~
中间没有用到数据库
希望有具体解释的回复
展开
 我来答
yanpuforever
推荐于2016-01-23
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
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 tb_Member set name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',sex='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',nPlace='"
+ ((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 tb_Member";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
sqlcon.Close();
}
写的有点乱,总之你要编辑需要三个事件:编辑,更新,取消
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
huzi01245
2009-05-21
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.Rows[e.NewEditIndex].Height = 18;
GridView1.Columns[1].ControlStyle.Width = 60;
GridView1.Columns[3].ControlStyle.Width = 60;
GridView1.Columns[5].ControlStyle.Width = 60;
GridView1.Columns[6].ControlStyle.Width = 60;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//修改记录时
{
string material_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string unit_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string supplier = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string material_area = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式