在点datagrid的编辑的时候出现“指定的转换无效”的错误:Dim projectid As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
请各位帮帮忙,急啊!!!!!!!!!!!!!!
--------------源码------
Sub dgResult_UpdateCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgResult.UpdateCommand
Dim intID As Integer = dgResult.DataKeys(e.Item.ItemIndex)
Dim projectid As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim myCommand As SqlCommand
strSql = "UPDATE Project Set projectid=@projectid WHERE id=" & intID & ""
myCommand.Parameters.Add(New SqlParameter("@projectid", SqlDbType.VarChar, 10))
myCommand.Parameters("@projectid").Value = projectid
myCommand = New SqlCommand(strSql, myConnection)
myConnection.Open()
Try
myCommand.ExecuteNonQuery()
dgResult.EditItemIndex = -1
Catch ex As SqlException
If ex.Number = 2627 Then
lblErrorMessage.Visible = True
lblErrorMessage.Text = "数据库中已经存在相同的项目标识!"
Else
lblErrorMessage.Visible = True
lblErrorMessage.Text = "不能更新记录!"
End If
End Try
myConnection.Close()
BindData()
试试
Dim projectid As String = (CType(e.Item.Cells(3).Controls(0), TextBox)).Text
你的dataGrid的Item.Cells(3).Controls(0不是TextBox
这样做试试:
将Dim projectid As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
改为
Dim projectText as TextBox=Ctype(e.Item.Cells(3).Controls(0),TextBox)
然后
Dim Projectid as String=projectText.Text
如果还不成,那应该就是
e.Item.Cells(3).Controls(0)
的问题了.
你可以将这个输入看看是什么...