注册 登录
编程论坛 ASP.NET技术论坛

求助!!

雪夜白狼 发布于 2008-05-15 21:20, 837 次点击
gridview的编辑功能
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        OleDbConnection conn;
        conn = DB.DBconntionadmin();
        conn.Open();

        string ID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        //GridViewRow row = this.GridView1.Rows[e.RowIndex]; //定义更新行对象
       string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
        string userpwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        string popedom = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;



        //string sql3 = "update admin set username='" + ((TextBox)(row.Cells[0].Controls[0])).Text.ToString() + "' and userpwd='" + ((TextBox)(row.Cells[1].Controls[0])).Text.ToString() + "' and popedom='" + ((TextBox)(row.Cells[2].Controls[0])).Text.ToString() + "' where id=" + ID;
        string sql3 = "update admin set username='" + username + "' and userpwd='" + userpwd + "' and popedom='" + popedom + "' where id=" + ID;
        OleDbCommand com = new OleDbCommand(sql3, conn);
        com.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        conn.Close();
        getbinder();

在红色字体处报错
无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
4 回复
#2
hebingbing2008-05-16 10:05
(GridView1.Rows[e.RowIndex].Cells[0].Controls[0]))
(GridView1.Rows[e.RowIndex].Cells[1].Controls[0]))
(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]))
这三次找到的控件是LinkButton不是textbox……
看看你的textbox的位置到底在gridview的什么地方……
#3
kevintang2008-05-16 11:03
#4
雪夜白狼2008-05-16 14:57
解决了啊
我加了几个textbox控件
#5
雪夜白狼2008-05-16 19:23
可是别的问题又来了啊!!
  string ID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        //GridViewRow row = this.GridView1.Rows[e.RowIndex]; //定义更新行对象
        string username = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("TextBox4")).Text;
        string userpwd = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox5")).Text;
        string popedom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox6")).Text;



string popedom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox6")).Text;

指定的参数已超出有效值的范围。
参数名: index
即便是能编辑的显示的也不是所填写的内容
显示不是0就是-1
是怎么回事呢?
1