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”。本人新手望高手指教。不胜感激 展开
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”。本人新手望高手指教。不胜感激 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询