GridView自带的编辑功能出了点问题,请教大家!

iloveyou19890403 2011-05-13 11:39:51
大家帮我看看哪里出问题了,代码如下:

//索引编辑时
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGridView();
}

//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
BindGridView();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
string sqlstr="update Products set ProductName='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',SupplierID='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',CategoryID='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',QuantityPerUnit='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',UnitPrice='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',UnitsInStock='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',UnitsOnOrder='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',ReorderLevel='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim() + "'Discontinued='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.ToString().Trim() + "' where ProductID='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

SqlCommand cmd = new SqlCommand(sqlstr,conn);
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGridView();

}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}

}

比如我把Discontinued 这个字段的值修改为“false”(原来是“true”),断点调试的时候,获取到“false”这个值了,但是没更新成功,怎么回事呀?
...全文
64 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yue547283947 2011-05-13
  • 打赏
  • 举报
回复
首先你点了更新,看数据库里的数据是否发生变化! 如果没变 就断点。。。一步一步的看问题在哪儿!

断点到更新的sql语句那里 把sql语句放在查询分析器里看看有效果没!
Tragedy 2011-05-13
  • 打赏
  • 举报
回复
你可以定义一个成员变量BindingSource bs = new BindingSource();作为中间变量,
绑定的时候给bs赋值,在把bs绑定到GridView上就行了.
iloveyou19890403 2011-05-13
  • 打赏
  • 举报
回复
绑定GridView的值了~
酷儿 2011-05-13
  • 打赏
  • 举报
回复
更没更新 你要看数据库字段的值 如果库里面更新成功了 就是你在更新之后没有重新绑定gridview的值
iloveyou19890403 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 andypanel 的回复:]

+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',ReorderLevel='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString(……
[/Quote]
呵呵,嗯,发现了,少了个逗号,汗。。。
AndyPanel 2011-05-13
  • 打赏
  • 举报
回复
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',ReorderLevel='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim() + "'Discontinued='"

Lz 你知道是哪里错了不?
因为Discontinued的前面少了个逗号.....
语句当然执行不成功啦
naonaoye 2011-05-13
  • 打赏
  • 举报
回复
可能BindGridView()有错?
iloveyou19890403 2011-05-13
  • 打赏
  • 举报
回复
嗯,我断点调试,查看数据的时候,修改的值获取到了,可是GridView绑定之后,查看数据,还是原来的那个值,不知道哪个地方出了小问题...
zhangyong361055720 2011-05-13
  • 打赏
  • 举报
回复
断点调试啊,看看是不是赋值了
naonaoye 2011-05-13
  • 打赏
  • 举报
回复
可以先执行后打开连接?
iloveyou19890403 2011-05-13
  • 打赏
  • 举报
回复
还是没解决,纠结了~

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧