请问,为什么用DataGrid在修改数据时提示“指定转换无效”?
源代码如下:(省略掉一些)namespace UseDataGrid
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected UseDataGrid.dsCustomers dsCustomers1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
bindData();
}
}
void bindData()
{
sqlDataAdapter1.Fill(dsCustomers1);
DataGrid1.DataBind();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
bindData();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string CustomerID=e.Item.Cells[1].Text;
string CompanyName=((TextBox)e.Item.Cells[2].Controls[0]).Text;
string ContactName=((TextBox)e.Item.Cells[3].Controls[0]).Text;
string ContactTitle=((TextBox)e.Item.Cells[4].Controls[0]).Text;
string Address=((TextBox)e.Item.Cells[5].Controls[0]).Text;
string City=((TextBox)e.Item.Cells[6].Controls[0]).Text;
string Region=((TextBox)e.Item.Cells[7].Controls[0]).Text;
string PostalCode=((TextBox)e.Item.Cells[8].Controls[0]).Text;
string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;
string Phone=((TextBox)e.Item.Cells[10].Controls[0]).Text;
string Fax=((TextBox)e.Item.Cells[11].Controls[0]).Text; sqlDataAdapter1.Fill(dsCustomers1);
dsCustomers.CustomersRow dr=dsCustomers1.Customers.FindByCustomerID(CustomerID);
dr.CompanyName=CompanyName;
dr.ContactName=ContactName;
dr.ContactTitle=ContactTitle;
dr.Address=Address;
dr.Region=Region;
dr.PostalCode=PostalCode;
dr.Country=Country;
dr.Phone=Phone;
dr.Fax=Fax;
sqlDataAdapter1.Update(dsCustomers1);
dsCustomers1.AcceptChanges();
DataGrid1.EditItemIndex=-1;
bindData();
} private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string CustomerID=e.Item.Cells[1].Text;
sqlDataAdapter1.Fill(dsCustomers1);
dsCustomers.CustomersRow dr=dsCustomers1.Customers.FindByCustomerID(CustomerID);
dr.Delete();
sqlDataAdapter1.Update(dsCustomers1);
dsCustomers1.AcceptChanges();
bindData();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
bindData();
} private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
LinkButton l=(LinkButton)e.Item.Cells[12].Controls[0];
DataRowView drv=(DataRowView)e.Item.DataItem;
l.Attributes.Add("onclick","javascript:return confirm('你确认要删除"+drv.Row.ItemArray[0]+"?');");
//Label label=(Label)e.Item.FindControl("LblID");
//label.Text=Convert.ToString(e.Item.ItemIndex+1);
}
}
}
}
运行时提示如下:
指定的转换无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 
行 187: string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;
行 188: string Phone=((TextBox)e.Item.Cells[10].Controls[0]).Text;
行 189: string Fax=((TextBox)e.Item.Cells[11].Controls[0]).Text;
行 190:
行 191: sqlDataAdapter1.Fill(dsCustomers1);请问这是什么原因呀?
谢谢大家了!

解决方案 »

  1.   

    DataGrid中是不是加有Link之类的控件?
      

  2.   

    什么Link控件?
    提示关键是这里出错:
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string CustomerID=e.Item.Cells[1].Text;
    string CompanyName=((TextBox)e.Item.Cells[2].Controls[0]).Text;
    string ContactName=((TextBox)e.Item.Cells[3].Controls[0]).Text;
    string ContactTitle=((TextBox)e.Item.Cells[4].Controls[0]).Text;
    string Address=((TextBox)e.Item.Cells[5].Controls[0]).Text;
    string City=((TextBox)e.Item.Cells[6].Controls[0]).Text;
    string Region=((TextBox)e.Item.Cells[7].Controls[0]).Text;
    string PostalCode=((TextBox)e.Item.Cells[8].Controls[0]).Text;
    string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;
    string Phone=((TextBox)e.Item.Cells[10].Controls[0]).Text;
    string Fax=((TextBox)e.Item.Cells[11].Controls[0]).Text; sqlDataAdapter1.Fill(dsCustomers1);
    dsCustomers.CustomersRow dr=dsCustomers1.Customers.FindByCustomerID(CustomerID);
    dr.CompanyName=CompanyName;
    dr.ContactName=ContactName;
    dr.ContactTitle=ContactTitle;
    dr.Address=Address;
    dr.Region=Region;
    dr.PostalCode=PostalCode;
    dr.Country=Country;
    dr.Phone=Phone;
    dr.Fax=Fax;
    sqlDataAdapter1.Update(dsCustomers1);
    dsCustomers1.AcceptChanges();
    DataGrid1.EditItemIndex=-1;
    bindData();
    }
    我也找不出是什么错误?
      

  3.   

    你确定你的代码是运行在Edit的状态下吗?
    在运行你上面的代码时行列对应的单元中确定是TextBox吗?
      

  4.   

    对,你有的列不是TexBotx类型的
      

  5.   

    行 187: string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;例如上面这一行如果出错你首先应该检查DataGrid的第10列中的第一个控件是不是TextBox类型的。其他的依此类推。
      

  6.   

    行 187: string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;
    行 188: string Phone=((TextBox)e.Item.Cells[10].Controls[0]).Text;没找到DataGrid这行中的TextBox控件 或是找到了 但控件类型不是Textbox
    _____________http://goody9807.611.cn/Boards.asp
      

  7.   

    可是我要怎么样才能把行转换成TextBox类型的呢?
      

  8.   

    goody9807() 、luckyprg(lucky) ,有在线吗?
    能不能帮小弟一下,谢谢了!
      

  9.   

    看你的列里面是不是TextBox了
    如果不是TextBox,就不能转为TextBox
    如果是Label,就把前面的(TextBox)改为(Label)就行,其它控件同理
      

  10.   

    我用模板列将每一列都绑定成 TextBox
    可是在更新的时候还是提示:指定转换无效
    请问是为什么?
    有那位大哥给做一个DataGrid编辑的Demo,谢谢!
      

  11.   

    行 187: string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;------------------------------------------------------------------------------------
    在这行前面加上
    Response.Write(e.Item.Cells[9].Controls[0].ID);先看看这个到底是什么?
      

  12.   

    我也曾经遇到过类似的问题,不过我看了下,跟我的不同,我的那时候是因为读取数据的方法不对!
    我觉得你的问题应该是在这里string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;,你应该把里边的TEXTBOX去掉,要是在单元格里有控件ID的话,最好用FINGDCONTROLS的方法
      

  13.   

    先在DATAGRID中把它转成模板列,再添加ID,再用FINDCONTROL引用