ifselectedindex changed , show data in a textbox

463 Views Asked by At

I've linked an access database to my form. I have 1 table , 2 rows 1 = Researchtype short text 2 = Researchdetails (long text)

In my combobox1 i've binded my researchtype row so i can choose a type of research.

Question now: how can i bind the details data to the richtextbox below it in order to show the research data as soon as i choose a research type?

I've tried if else combos, try catch combos, i'm thinking i'm actually overthinking the issue here.

What would be the easiest way to "select from dropdown" and show the result in textbox. I'm a vb.net beginner

Public Class Onderzoeken
    Private Sub Onderzoeken_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PatientenDatabaseDataSetX.tbl_OnderzoeksTypes' table. You can move, or remove it, as needed.
        Me.Tbl_OnderzoeksTypesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_OnderzoeksTypes)
    End Sub

    Private Sub cboxOnderzoek_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxOnderzoek.SelectedIndexChanged
        If cboxOnderzoek.SelectedItem = Nothing Then

            cboxOnderzoek.Text = ""
        Else
            rtbBeschrijvingOnderzoek.Text = CStr(CType(cboxOnderzoek.SelectedItem, DataRowView)("OZ_Onderzoeksbeschrijving"))

        End If

    End Sub
End Class

I added the entire code of that page now , it's not much, but as stated: I added the binding source and displaymember "researchtype" to the combobox. So when i start the form, i can choose a type of research. Now i need to show the description of the research in the richtextbox

2

There are 2 best solutions below

1
Mary On BEST ANSWER

In the Form.Load...

I have a function that returns a DataTable that contains columns called Name and Type. I bind the ComboBox to the DataTable and set the DisplayMember to "Name". Each Item in the ComboBox contains the entire DataRowView. I set the TextBox to the first row (dt(0)("Type")) Type column value so the correct information will be displayed for the initial selection.

I put the code to change the textbox display in ComboBox1.SelectionChangeCommitted because the other change events will produce a NRE since .SelectedItem has not yet been set when the form loads. The commited event will only occur when the user makes a selection.

First, cast the SelectedItem to its underlying type, DataRowView. Then you want the value of the Type column. This value is assigned to the text property of the textbox.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt = LoadCoffeeTable()
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "Name"
    TextBox1.Text = dt(0)("Type").ToString
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    TextBox1.Text = DirectCast(ComboBox1.SelectedItem, DataRowView)("Type").ToString
End Sub

Just substitute Researchtype for Name and Researchdetails for Type.

1
Xingyu Zhao On

After using 'OleDbDataAdapter' to fill the dataset, you can set 'DisplayMember' and 'ValueMember' for your ComboBox. Every time the index of your ComboBox changes, it's 'ValueMember' will be displayed in richtextbox.

Here's the code you can refer to.

Private dataset As DataSet = New DataSet()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim connString As String = "your connection String"
    Using con As OleDbConnection = New OleDbConnection(connString)
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT Researchtype, Researchdetails FROM yourtable"
        Dim adpt As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        adpt.Fill(dataset)
    End Using
    ComboBox1.DisplayMember = "Researchtype"
    ComboBox1.ValueMember = "Researchdetails"
    ComboBox1.DataSource = dataset.Tables(0)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    RichTextBox1.Text = ComboBox1.SelectedValue.ToString()
End Sub

Result of my test.

enter image description here