Question

I have a datagridview and combobox on a form. I wish to move between rows of datagridview and combobox should be synchronized with the value of one of the columns of the dataset datagridview. In combobox I have a list with two possible values​​, Rational (1) and Irrational (2). The column "TYPE" of the dataset refers to previous types. Below is the code, but not getting the desired result. What is wrong?

Thanks, Davis

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
Was it helpful?

Solution

your code should work well to fill datagridview and combobox

what else you want the code to do ?

what do you mean by (desired result) ,

is it to change the combobox value when you change the selected row in datagridview ?

if so . . . then you have to handle the SelectedIndexChanged

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top