private void DataGrid2_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int artid=(int)DataGrid2.DataKeys[e.Item.ItemIndex];
TextBox newtitle=(TextBox)e.Item.FindControl("tbtitle");
TextBox newcontent=(TextBox)e.Item.FindControl("tbcontent");
TextBox newclassname=(TextBox)e.Item.FindControl("tbclassname");
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
string sql="update db_article set title='"+newtitle.Text+"',content='"+newcontent.Text+",classname='"+newclassname.Text+"' where articleid="+artid;
SqlCommand comm=new SqlCommand(sql,conn);
conn.Open();
comm.ExecuteNonQuery();
DataGrid2.EditItemIndex= -1;
getxg(); //这里是一个数据的绑定的公共函数; }我写的一个dataGrid修改,更新,编辑。但当点更新后,出现如下提示:指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 73: private void DataGrid2_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 74: {
行 75: int artid=(int)DataGrid2.DataKeys[e.Item.ItemIndex];
行 76: TextBox newtitle=(TextBox)e.Item.FindControl("tbtitle");
行 77: TextBox newcontent=(TextBox)e.Item.FindControl("tbcontent");大家说说我这段代码错在哪里啊???
{
int artid=(int)DataGrid2.DataKeys[e.Item.ItemIndex];
TextBox newtitle=(TextBox)e.Item.FindControl("tbtitle");
TextBox newcontent=(TextBox)e.Item.FindControl("tbcontent");
TextBox newclassname=(TextBox)e.Item.FindControl("tbclassname");
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
string sql="update db_article set title='"+newtitle.Text+"',content='"+newcontent.Text+",classname='"+newclassname.Text+"' where articleid="+artid;
SqlCommand comm=new SqlCommand(sql,conn);
conn.Open();
comm.ExecuteNonQuery();
DataGrid2.EditItemIndex= -1;
getxg(); //这里是一个数据的绑定的公共函数; }我写的一个dataGrid修改,更新,编辑。但当点更新后,出现如下提示:指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 73: private void DataGrid2_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 74: {
行 75: int artid=(int)DataGrid2.DataKeys[e.Item.ItemIndex];
行 76: TextBox newtitle=(TextBox)e.Item.FindControl("tbtitle");
行 77: TextBox newcontent=(TextBox)e.Item.FindControl("tbcontent");大家说说我这段代码错在哪里啊???
TextBox newtitle=(TextBox)e.Item.FindControl("tbtitle").Text; ??
newtitle.text=((TextBox) e.Item.FindControl("tbtitle")).Text主要是类型转换问题!
我觉得他的是对的
请问你在数据绑定的时候,有没有设定键值列?如果没有设,此处取必须出错.
如果有设,你修改如下 看看
int artid = Convert.ToInt16(DataGrid2.DataKeys[e.Item.ItemIndex].ToString());
2.后面的两个取对象的语句没有错.
如果上述问题均修改完毕但还是错,那么把那两句取对象的修改为:
TextBox newtitle=(TextBox)DataGrid2.Item[e.Item.ItemIndex].FindControl("tbtitle");
TextBox newcontent=(TextBox)DataGrid2.Item[e.Item.ItemIndex].FindControl("tbcontent");
3.如果上述修改仍然出错,你可用如下语句,看看你的对象到底是什么类型
this.Response.Write(DataGrid2.Item[e.Item.ItemIndex].FindControl("tbcontent").GetType().ToString());
this.Response.End();
' Get the text box that contains the price to edit.
' For bound columns the edited value is stored in a text box.
' The text box is the first control in the Controls collection.
Dim priceText As TextBox = e.Item.Cells(3).Controls(0) ' Get the check box that indicates whether to include tax from the
' TemplateColumn. Notice that in this case, the check box control is
' second control in the Controls collection.
Dim taxCheck As CheckBox = e.Item.Cells(2).Controls(1) Dim item As String = e.Item.Cells(1).Text
Dim price As String = priceText.Text
Dim dr As DataRow ' With a database, use an update command. Since the data source is
' an in-memory DataTable, delete the old row and replace it with a new one. ' Remove old entry.
StoreView.RowFilter = "Item='" & item & "'"
If StoreView.Count > 0 Then
StoreView.Delete(0)
End If
StoreView.RowFilter = ""
' Add new entry.
dr = Store.NewRow() If taxCheck.Checked Then
dr(0) = "8.6%"
Else
dr(0) = "0.0%"
End If
dr(1) = item
dr(2) = price
Store.Rows.Add(dr) MyDataGrid.EditItemIndex = -1
BindGrid() End Sub Sub BindGrid()
MyDataGrid.DataSource = StoreView
MyDataGrid.DataBind()
End Sub
--------------------------
这句明显错误啊.
content='"+newcontent.Text+",的'呢?int artid=(int)DataGrid2.DataKeys[e.Item.ItemIndex];
--------------------------
应该保证前台的DataGrid有DataKeyField="artid"
这个取得主索引键之值是不是要在控件设什么?