标题:关于gridview编辑状态下写入数据库的问题
只看楼主
moderndai
Rank: 1
等 级:新手上路
帖 子:104
专家分:0
注 册:2006-1-25
 问题点数:0 回复次数:1 
关于gridview编辑状态下写入数据库的问题

gridview下如何实现数据写入,分2种情况,1种是将BoundField一种是TemplateField
不知道我自己写的代码哪里出错了,总是不能写入数据库。点击更新没有反映
我写的代码是
protected void gvw_Drink_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//boundfield
string DrinkName = ((TextBox)gvw_Drink.Rows[e.RowIndex].FindControl("TextBox2")).Text;//boundfield
//string DrinkEng = ((TextBox)gvw_Drink.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
//模版状态
string DrinkEng = gvw_Drink.Rows[e.RowIndex].Cells[2].Text;
string DrinkPri = gvw_Drink.Rows[e.RowIndex].Cells[3].Text;//模版状态
Common com = new Common();
com.Open();
SqlCommand cmd_DrinkUpdate = new SqlCommand("update drink set drink_name='"+DrinkName+"',drink_eng='"+DrinkEng+"',drink_pri='"+DrinkPri+"' where drink_id='"+Convert.ToInt32(this.gvw_Drink.DataKeys[e.RowIndex].Value.ToString())+"'", com.myConnection);
cmd_DrinkUpdate.ExecuteNonQuery();
com.Close();
this.gvw_Drink.EditIndex = -1;
BindToGridView();
}

搜索更多相关主题的帖子: 数据库 gridview gvw 状态 Drink 
2007-05-02 11:02
moderndai
Rank: 1
等 级:新手上路
帖 子:104
专家分:0
注 册:2006-1-25
得分:0 

一天了都没人回,自己调试搞定了!
protected void gvw_Drink_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string DrinkName = ((TextBox)gvw_Drink.Rows[e.RowIndex].FindControl("txt_DrinkName")).Text.Trim();
string DrinkEng = ((TextBox)gvw_Drink.Rows[e.RowIndex].FindControl("txt_DrinkEng")).Text.Trim();
string DrinkPri = ((TextBox)gvw_Drink.Rows[e.RowIndex].FindControl("txt_DrinkPri")).Text.Trim();
Common com = new Common();
com.Open();
SqlCommand cmd_DrinkUpdate = new SqlCommand("update drink set drink_name='"+DrinkName+"',drink_eng='"+DrinkEng+"',drink_pri='"+DrinkPri+"' where drink_id='"+Convert.ToInt32(this.gvw_Drink.DataKeys[e.RowIndex].Value.ToString())+"'", com.myConnection);
cmd_DrinkUpdate.ExecuteNonQuery();
com.Close();
this.gvw_Drink.EditIndex = -1;
BindToGridView();
}

ROCK AND ROCK PROGRAMING dai--yl@&&espricle@
2007-05-02 20:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-136532-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017712 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved