| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:[求助]为什么我的datagrid控件无法更新.
只看楼主 加入收藏
youran415
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-7
收藏
 问题点数:0 回复次数:2 
[求助]为什么我的datagrid控件无法更新.
到网上搜了很多文章都没有解决,我是新人,大家多多帮忙。
代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bindgrid();
}
}
public void Bindgrid()
{
string conn="server=LZH;uid=sa;pwd=sa;database=sms";
SqlConnection myconn=new SqlConnection(conn);
myconn.Open();
string strsql="select Student_id,Student_name,Student_sex,Student_nation,Student_birthday,Student_time,Student_classid,Student_home,Student_else from student";
SqlDataAdapter da=new SqlDataAdapter(strsql,myconn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_student.DataSource=ds;
Dgd_student.DataBind();
myconn.Close();


}
private void Dgd_student_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string conn="server=LZH;uid=sa;pwd=sa;database=sms";
SqlConnection myconn=new SqlConnection(conn);
string strsql="update student set Student_name=@Student_name,Student_sex=@Student_sex,Student_nation=@Student_nation,Student_birthday=@Student_birthday,Student_time=@Student_time,Student_classid=@Student_classid,Student_home=@Student_home,Student_else=@Student_else where Student_id=@Student_id";
SqlCommand cm=new SqlCommand(strsql,myconn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@Student_name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_sex",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_nation",SqlDbType.Char,5));
cm.Parameters.Add(new SqlParameter("@Student_birthday",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_time",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_classid",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_home",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_else",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@Student_name"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@Student_sex"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[3].Controls[0]).Text;
cm.Parameters["@Student_nation"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@Student_birthday"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[5].Controls[0]).Text;
cm.Parameters["@Student_time"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[6].Controls[0]).Text;
cm.Parameters["@Student_classid"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[7].Controls[0]).Text;
cm.Parameters["@Student_home"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[8].Controls[0]).Text;
cm.Parameters["@Student_else"].Value=colvalue;
cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_student.EditItemIndex=-1;

}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";

}
cm.Connection.Close();
Bindgrid();
}
搜索更多相关主题的帖子: datagrid 控件 
2006-03-07 17:29
youran415
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-7
收藏
得分:0 

点更新后在textbox中更新数据后显示的还是原来的数据并提示“编辑出错”,也就是我在catch中写的提示,请问是什么原因,帮忙,谢谢


俺是超级无敌大菜鸟.
2006-03-07 17:31
youran415
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-7
收藏
得分:0 

没人?斑竹也不常来吗?


俺是超级无敌大菜鸟.
2006-03-08 11:18
快速回复:[求助]为什么我的datagrid控件无法更新.
数据加载中...
 
   



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

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