Vb.net 编辑按钮:从字符串转换";名称“;在datagrid视图中

Vb.net 编辑按钮:从字符串转换";名称“;在datagrid视图中,vb.net,Vb.net,我正在尝试编辑Datagridview中的选定行。当我试图更改文本框中的某些内容时,我会收到一条关于名字和姓氏文本框的错误消息,指出:“从字符串“name”转换为整数是无效的。如果有人能帮我找出我做错了什么,我将不胜感激 Dim dbo As New OwnersDataContext Dim theowner As New Owners Dim index As Integer Private Sub DGV1_CellContentClick(sender As Object, e As

我正在尝试编辑Datagridview中的选定行。当我试图更改文本框中的某些内容时,我会收到一条关于名字和姓氏文本框的错误消息,指出:“从字符串“name”转换为整数是无效的。如果有人能帮我找出我做错了什么,我将不胜感激

Dim dbo As New OwnersDataContext
Dim theowner As New Owners
Dim index As Integer

Private Sub DGV1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentClick
    index = e.RowIndex
    Dim selectedRow As DataGridViewRow

    selectedRow = DGV1.Rows(index)
    txtid.Text = selectedRow.Cells(0).Value.ToString
    txtfname.Text = selectedRow.Cells(1).Value.ToString
    txtlname.Text = selectedRow.Cells(2).Value.ToString
    txtaddress.Text = selectedRow.Cells(3).Value.ToString
    txtcity.Text = selectedRow.Cells(4).Value.ToString
    cbostates.SelectedItem = selectedRow.Cells(5).Value.ToString
    txtzipcode.Text = selectedRow.Cells(6).Value.ToString
End Sub

Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
    If DGV1.SelectedRows.Count < 0 Then
        MessageBox.Show("Select owner")
    Else

            **If theowner.Update(txtid.Text, txtfname.Text, txtlname.Text, txtaddress.Text, txtcity.Text, CStr(cbostates.SelectedItem), txtzipcode.Text) = 0** Then
                MessageBox.Show("New Owner could not be saved to database. Please check input.")
            Else
                Me.Close()
                'refill the tbl with the updated
                Dim q = From o In dbo.Owners
                        Select o.Owner_ID, o.Owner_FirstName, o.Owner_LastName, o.Address, o.City, o.State, o.ZipCode
                DGV1.DataSource = q
            End If

    End If
End Sub

另外,我看到你已经得到了之前一些问题的答案。你应该给他们一些反馈,让那些花时间帮助你的人知道他们的努力没有白费。如果一个答案解决了你的问题,你应该按他们帖子左侧的勾号/复选标记将其标记为接受。更多信息关于这一点的信息可以在这里找到:我仍然需要查看that
Update()
方法,因为它(或它的代码)是导致错误的原因。您刚才添加的代码与问题无关。至于问题,如果该行上有一个
字符串
被转换为
整数
,那么这是一个隐式转换,这意味着您必须关闭
选项
。将其打开
,编译器将立即启动标记您在何处传递错误类型的数据。然后,您可以通过适当的强制转换或转换来解决这些问题。显然,当需要数字时,您正在某处使用名称。我怀疑您对该
Update
方法的参数顺序错误,但如果实际上,您可以查看该方法。实际上,查看您在评论中发布的代码,
pOwnerID
pZipCode
都声明为
Integer
,您将
字符串
传递给这两个参数,因此几乎可以肯定它是其中之一。其中一个
文本框
似乎包含文本“名称“。也许您应该在保存数据之前尝试验证数据。这就是为什么要使用
Integer.TryParse
将文本转换为
Integer
:它一次验证并转换
选项Strict On
强制您进行转换,您可以输入验证以获得良好的度量值。永远不要假设用户会做正确的事情;您发布的
Update
函数声明为type
Integer
,但它返回的是
布尔值<代码>选项严格限制
也会抓住这样的愚蠢。数据类型很重要。它们不是武断的。
 Public Function Update(ByVal pOwnerID As Integer, ByVal pfname As String, 
ByVal plname As String, ByVal pAddress As String, ByVal pCity As String, ByVal pState As String, ByVal pZipCode As Integer) As Integer

 Return mytableadapter.Update(pfname, plname, pAddress, pCity, pState, pZipCode, pOwnerID) > 0
 End Function