请问,为什么用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);请问这是什么原因呀?
谢谢大家了!
源代码如下:(省略掉一些)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);请问这是什么原因呀?
谢谢大家了!
提示关键是这里出错:
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();
}
我也找不出是什么错误?
在运行你上面的代码时行列对应的单元中确定是TextBox吗?
行 188: string Phone=((TextBox)e.Item.Cells[10].Controls[0]).Text;没找到DataGrid这行中的TextBox控件 或是找到了 但控件类型不是Textbox
_____________http://goody9807.611.cn/Boards.asp
能不能帮小弟一下,谢谢了!
如果不是TextBox,就不能转为TextBox
如果是Label,就把前面的(TextBox)改为(Label)就行,其它控件同理
可是在更新的时候还是提示:指定转换无效
请问是为什么?
有那位大哥给做一个DataGrid编辑的Demo,谢谢!
在这行前面加上
Response.Write(e.Item.Cells[9].Controls[0].ID);先看看这个到底是什么?
我觉得你的问题应该是在这里string Country=((TextBox)e.Item.Cells[9].Controls[0]).Text;,你应该把里边的TEXTBOX去掉,要是在单元格里有控件ID的话,最好用FINGDCONTROLS的方法