首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

GridView 编辑功能 中的 "更新" 有关问题 请高手帮助! !@

2012-02-13 
GridView编辑功能中的 更新 问题请高手帮助!! 在线等!!@这是c#中更新的代码:(不知道哪错了)protectedvoi

GridView 编辑功能 中的 "更新" 问题 请高手帮助!! 在线等!!@
这是c#中     更新的代码:   (不知道哪错了)

protected   void   GridView1_RowUpdating(object   sender,   GridViewUpdateEventArgs   e)
        {
                String   sqlstr,   id,   GridView1,rs1,rs2,rs3;
                int   i;
                TextBox   obj1,   obj2,   obj3;
                i   =   GridView1.DataKeys[e.Item.ItemIndex];
                obj1   =   (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
                obj2   =   (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
                obj3   =   (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];

           


                rs1   =   obj1.Text;
                rs2   =   obj2.Text;
                rs3   =   obj3.Text;
                id   =   GridView1.DataKeys[i].ToString();
                              myCon   =   new   SqlConnection(strCon);
                string   sqlstr   =   "update   data   set   Name= ' "+   rs3   + " ',Title= ' "+   rs2   + " ',Content= ' "+   rs1   + " '   where   id= "+   id   ;
                SqlCommand   rs   =   new   SqlCommand(sqlstr,   myCon);
                myCon.Open();
                rs.ExecuteNonQuery();
                myCon.Close();
                GridView1.EditItemIndex   =   -1;
                bind();
        }

[解决办法]
错了饿..是 e.Keys[ "字段名 "].ToString();
[解决办法]
id取值不用那么麻烦的,直接就能取到了,用下面的代码试试看
int id = e.RowIndex + 1;
string rs3 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string rs2 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string rs1 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
myCon = new SqlConnection(strCon);
myCon.Open();
SqlCommand cmd = new SqlCommand( "update data set name= ' " + rs3 + " ',Title ' " + rs2 + " ',Content= ' " + rs1 + " ' where id= ' " + Convert.ToString(id) + " ' ", con);
cmd.ExecuteNonQuery();
Response.Write( "ok ");
[解决办法]
Jiaowei85() ,您的回言:int id = e.RowIndex + 1;


string rs3 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string rs2 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string rs1 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
myCon = new SqlConnection(strCon);
myCon.Open();
SqlCommand cmd = new SqlCommand( "update data set name= ' " + rs3 + " ',Title ' " + rs2 + " ',Content= ' " + rs1 + " ' where id= ' " + Convert.ToString(id) + " ' ", con);
cmd.ExecuteNonQuery();
Response.Write( "ok ");
但是我觉得这里你得到的id并不是记录的id号呀,感觉你得到的id就是在显示出来的结果里面的这些字段的编号,就是从1到总数,可能这里标号为1的记录在数据库里面她的id却是100,是存在这个问题?还是是我自己理解错误了?

热点排行