0

我在表单上有一个 datagridview 和组合框。我希望在 datagridview 的行之间移动,并且组合框应该与数据集 datagridview 的列之一的值同步。在组合框中,我有一个包含两个可能值的列表,Rational (1) 和 Irrational (2)。数据集的“TYPE”列是指以前的类型。下面是代码,但没有得到想要的结果。怎么了?

谢谢,戴维斯

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
4

1 回答 1

1

您的代码应该可以很好地填充 datagridview 和组合框

你还想让代码做什么?

你是什​​么意思(期望的结果),

更改 datagridview 中的选定行时是否更改组合框值?

如果是这样 。. . 那么你必须处理SelectedIndexChanged

于 2013-06-10T07:32:18.757 回答