gridview 编辑时出现System.InvalidCastException异常

usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS... using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //引入数据库名称空间

public partial class GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public void getData()
{
//创建SqlConnection连接字符串
SqlConnection scn = new SqlConnection("server=localhost;database=MyDatabase;user=grid;password=gridview");
//创建SqlCommand数据库操作命令
SqlCommand cmd = new SqlCommand("SELECT * FROM USERINFO", scn);

//创建SqlDataAdapter数据库适配器
SqlDataAdapter da = new SqlDataAdapter(cmd);
//创建DataSet数据集(映射足够大的数据库)
DataSet ds = new DataSet();

//和数据库进行交互
da.Fill(ds, "USERINFO");

//为GridView控件绑定数据源
this.GridViewShow.DataSource = ds.Tables[0].DefaultView;
this.GridViewShow.DataBind();
}
protected void btnShow_Click(object sender, EventArgs e)
{
getData();
}
protected void GridViewShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridViewShow.PageIndex = e.NewPageIndex;
getData();
}
protected void GridViewShow_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{

}
protected void GridViewShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//删除行
}
protected void GridViewShow_RowEditing(object sender, GridViewEditEventArgs e)
{
//编辑行
this.GridViewShow.EditIndex = e.NewEditIndex;
getData();
}
protected void GridViewShow_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//提交
//取数据
//以下方法取到的是数据库的老数据
//string name = this.GridViewShow.DataKeys[e.RowIndex][0].ToString();
//取编辑后的数据
string name = ((TextBox)this.GridViewShow.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
}
protected void GridViewShow_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//取消
this.GridViewShow.EditIndex = -1;
getData();
}
public void updateUsers(string name,string birth,string sex,string ID)
{

}
}

就是string name = ((TextBox)this.GridViewShow.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();出现"无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。本人新手望高手指教。不胜感激
展开
 我来答
五千个字节
2010-02-16 · TA获得超过205个赞
知道小有建树答主
回答量:407
采纳率:0%
帮助的人:233万
展开全部
控件没找对
string name = ((TextBox)this.GridViewShow.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString()中的索引0对吗?他是第一个控件吗?改为
((TextBox)this.GridViewShow.Rows[e.RowIndex].FindControl("控件ID名")).Text.ToString()
看看可以吗?
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式