我正在尝试从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
添加了我班上的更新方法
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