我正在使用一个数据库,我的添加表单和编辑表单有两个问题。
我的第一个问题是我的添加表单。我不断收到错误
从类型"数据行视图"到类型"字符串"的转换无效
我已经在不断收到错误消息的地方加粗了。这是我在添加表单下的代码:
Public Class Add_New_University
Public Property universityid As Integer
Private myuniversities As New University
Private Sub Add_New_University_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' TODO: This line of code loads data into the '_UniversityCourses_MDFDataSet.University' table. You can move, or remove it, as needed.
Me.UniversityTableAdapter.Fill(Me._UniversityCourses_MDFDataSet.University)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
'NOTE: Exception thrown on this line.
If myuniversities.Insert((txtuniversity.Text), CStr(cbo1.SelectedValue), CStr(cbo2.SelectedValue), (txttuition.Text)) Then
Me.Close()
Else
MessageBox.Show("Cannot update the University table.")
End If
End Sub
' closes the form
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
我的第二个问题是在我的编辑表单中:我写了一个尝试捕获,应该将输入的信息更新到 dgv 中,否则它会抛出错误
无法更新大学表。
我不断收到其他消息,并且没有任何东西添加到我的 dgv 中。
这是我的编辑表单代码:
Public Class EditUniversity
Public Property UniversityId As Integer
Private myuniversities As New University
Private Sub EditUniversity_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' TODO: This line of code loads data into the '_UniversityCourses_MDFDataSet.University' table. You can move, or remove it, as needed.
Me.UniversityTableAdapter.Fill(Me._UniversityCourses_MDFDataSet.University)
End Sub
' Save Button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Validate input
If txtuniversity.Text = "" Or IsNumeric(txtuniversity.Text) = True Then
MessageBox.Show("Please enter a valid university name.")
txtuniversity.Clear()
txtuniversity.Focus()
ElseIf txttuition.Text = "" Or IsNumeric(txttuition.Text) = False Then
MessageBox.Show("Please enter a valid amount.")
txttuition.Clear()
txttuition.Focus()
End If
Try
myuniversities.Update(UniversityId, CStr(txtuniversity.Text), CStr(cbo1.SelectedItem), CStr(cbo2.SelectedItem), CDec(txttuition.Text))
Me.Close()
Catch ex As Exception
MessageBox.Show("Cannot update the University table.")
End Try
End Sub
'Close Button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
如果有人能帮助我了解我做错了什么,我们将不胜感激。谢谢!
我将解决第一个问题,因为它与这个问题的标题有关,并忽略第二个问题,它应该移动到另一个具有适当标题的线程。
SelectedValue
属性返回与SelectedItem
属性相同的事实表明您尚未设置ValueMember
属性。 正如DisplayMember
指定应从中绘制数据的绑定DataTable
的DataColumn
以显示在控件中一样,ValueMember
指定应从中绘制数据以通过SelectedValue
公开的DataColumn
。
也就是说,如果您只想在控件中显示文本,则可以使用Text
属性。 但是,通常,您会显示一个用户友好的列,例如名称或描述,然后通过SelectedValue
访问相应的主键值。