.net 带有dataset和bindingsource的组合框。我做错了什么?
我在表单上有一个datagridview和combobox。我希望在datagridview的行之间移动,combobox应该与dataset datagridview的一列的值同步。在combobox中,我有一个包含两个可能值的列表, 理性(1)和非理性(2)。数据集的“类型”列引用以前的类型。 下面是代码,但没有得到期望的结果。怎么了 谢谢, 戴维斯.net 带有dataset和bindingsource的组合框。我做错了什么?,.net,vb.net,binding,combobox,bindingsource,.net,Vb.net,Binding,Combobox,Bindingsource,我在表单上有一个datagridview和combobox。我希望在datagridview的行之间移动,combobox应该与dataset datagridview的一列的值同步。在combobox中,我有一个包含两个可能值的列表, 理性(1)和非理性(2)。数据集的“类型”列引用以前的类型。 下面是代码,但没有得到期望的结果。怎么了 谢谢, 戴维斯 您的代码应该能够很好地填充datagridview和combobox 你还想让代码做什么 你所说的(期望的结果)是什么意思 在datagr
您的代码应该能够很好地填充datagridview和combobox 你还想让代码做什么 你所说的(期望的结果)是什么意思 在datagridview中更改所选行时是否更改组合框值?
如果是的话。然后您必须处理SelectedIndexChanged是的,这是所需的结果,但更改到“TYPE”字段具有1或2代码的下一行时,组合框不会显示当前值“Rational”或“Unrational”。
Public Class Form1
Private BdgSource As BindingSource = New BindingSource
Private StrCon As String = "Server=LOCALHOST:50000;Database=SAMPLE;UID=USERX;PWD=YYY;Connect Timeout=30"
Private Dts As DataSet = New DataSet
Private Class TypeAnimal
Private _Type As Integer = 0
Private _Desc As String = ""
Property Type As Integer
Set(ByVal value As Integer)
_Type = value
End Set
Get
Return _Type
End Get
End Property
Property Description As String
Set(ByVal value As String)
_Desc = value
End Set
Get
Return _Desc
End Get
End Property
Public Sub New(ByVal Type As Integer, ByVal Description As String)
_Type = Type
_Desc = Description
End Sub
End Class
Private Sub UpdateDatagridView()
Dim con As DB2Connection = New DB2Connection(StrCon)
'Dim trans As DB2Transaction
Dim cmd As DB2Command = New DB2Command
Dim adp As DB2DataAdapter = New DB2DataAdapter
con.Open()
cmd.Transaction = con.BeginTransaction
cmd.Connection = con
cmd.CommandText = "SELECT ANIMAL, TYPE FROM TESTE"
'cmd.ExecuteNonQuery()
adp.SelectCommand = cmd
adp.Fill(Dts, "TABLE1")
BdgSource.DataSource = Dts.Tables(0)
DataGridView1.DataSource = BdgSource.DataSource
End Sub
Private Sub FillCombobox()
Dim lista As List(Of TypeAnimal) = New List(Of TypeAnimal)
lista.Add(New TypeAnimal(1, "Rational"))
lista.Add(New TypeAnimal(2, "Irrational"))
ComboBox1.DataSource = lista
ComboBox1.DisplayMember = "Description"
ComboBox1.ValueMember = "Type"
End Sub
Private Sub BindComponents()
Dim bd As Binding = New Binding("SelectedValue", BdgSource, "TYPE")
ComboBox1.DataBindings.Add(bd)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UpdateDatagridView()
FillCombobox()
BindComponents()
End Sub
End Class