不写下拉框列表选择项改变事件时一切正常,省份正确读取并绑定到下拉框列表中,但
写了下拉框列表选择项改变事件后,就报错了,错误:从类型“DataRowView”到类型“Integer”的转换无效。对应红色字体
Private Sub ProviceCityForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim conStr As String
conStr = "Data Source=.;Initial Catalog=MyTestDB;Integrated Security=True"
Dim con As New SqlConnection(conStr)
'查询所有省份
Dim sql As String
sql = "SELECT * FROM [MyTestDB].[dbo].[provinceCity] WHERE ParentId = 0"
Dim adapter As New SqlDataAdapter(sql, con)
Dim ds As New DataSet()
adapter.Fill(ds)
'绑定所有省份下拉列表框
Me.cboProvice.DataSource = ds.Tables(0)
Me.cboProvice.DisplayMember = "Name"
Me.cboProvice.ValueMember = "ID"
End Sub
Private Sub cboProvice_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboProvice.SelectedIndexChanged
Dim conStr As String
conStr = "Data Source=.;Initial Catalog=MyTestDB;Integrated Security=True"
Dim con As New SqlConnection(conStr)
'查询所有省份
Dim pId As Integer
pId = Me.cboProvice.SelectedValue
Dim sql As String
sql = "SELECT * FROM [MyTestDB].[dbo].[provinceCity] WHERE ParentId = " & pId
Dim adapter As New SqlDataAdapter(sql, con)
Dim ds As New DataSet()
adapter.Fill(ds)
'绑定所有省份下拉列表框
Me.cboCity.DataSource = ds.Tables(0)
Me.cboCity.DisplayMember = "Name"
Me.cboCity.ValueMember = "ID"
End Sub
11 个解决方案
#1
Me.cboProvice.SelectedValue,这是你下拉框中选中的数据吧,是数字吗?
#2
嗯,是的,是从数据库读出来的ID值。
#3
调试时,发现经过Load事件的
这句话就跳到红色字体。
#4
图片指:
'绑定所有省份下拉列表框 下面的那句话:Me.cboProvice.DataSource = ds.Tables(0)
#5
改为Me.cboProvice.text
#6
搞错了,没仔细看改为
#7
谁知道怎么弄?
#8
下拉框用绑定的话读出来的项类型是DataRowView.
Dim pId As Integer
Dim rv As DataRowView
rv = Me.cboProvice.SelectedValue
pId = rv("列名")
#9
在load的事件中,开始的地方删除cboProvice_SelectedIndexChanged的监听,在结束处在添加上
#10
回复ChataoCoding:这样写过后,执行到 Me.cboProvice.ValueMember = "ID"时,跳到
rv = Me.cboProvice.SelectedValue这句话,还是报错:无法将int32类型强制转换成DataRowView类型
回复hmc108:怎么删除cboProvice_SelectedIndexChanged的监听?具体代码有吗?
rv = Me.cboProvice.SelectedValue这句话,还是报错:无法将int32类型强制转换成DataRowView类型
回复hmc108:怎么删除cboProvice_SelectedIndexChanged的监听?具体代码有吗?
#11
已解决,受ChataoCoding启发。谢谢大家。
#1
Me.cboProvice.SelectedValue,这是你下拉框中选中的数据吧,是数字吗?
#2
嗯,是的,是从数据库读出来的ID值。
#3
调试时,发现经过Load事件的
这句话就跳到红色字体。
#4
图片指:
'绑定所有省份下拉列表框 下面的那句话:Me.cboProvice.DataSource = ds.Tables(0)
#5
改为Me.cboProvice.text
#6
搞错了,没仔细看改为
#7
谁知道怎么弄?
#8
下拉框用绑定的话读出来的项类型是DataRowView.
Dim pId As Integer
Dim rv As DataRowView
rv = Me.cboProvice.SelectedValue
pId = rv("列名")
#9
在load的事件中,开始的地方删除cboProvice_SelectedIndexChanged的监听,在结束处在添加上
#10
回复ChataoCoding:这样写过后,执行到 Me.cboProvice.ValueMember = "ID"时,跳到
rv = Me.cboProvice.SelectedValue这句话,还是报错:无法将int32类型强制转换成DataRowView类型
回复hmc108:怎么删除cboProvice_SelectedIndexChanged的监听?具体代码有吗?
rv = Me.cboProvice.SelectedValue这句话,还是报错:无法将int32类型强制转换成DataRowView类型
回复hmc108:怎么删除cboProvice_SelectedIndexChanged的监听?具体代码有吗?
#11
已解决,受ChataoCoding启发。谢谢大家。