'Combobox with values from database won't display passed value from Datagridview row even if the values are the same

I have a Combobox1 in a form that loaded with values from database. The values loaded perfectly into the Combobox1. In the process of updating the info of a student, I have a row in the datagridview that has a button "Edit", when I click the "Edit" button, it shows up a form with textboxes and comboboxes that loaded with all the values from the Datagridview row, pertaining to that specific student. In the form load event, I loaded data from database going to that Combobox1. When the form shows, the Combobox1 display the first item from the database query, not the value from the Datagridview that I selected to pass to the Combobox1 even if they are they same as the item from the query.

Here is the code for fetching data

 Public Sub LoadSections(cb As ComboBox)

        Try
            sql = "SELECT ID, section_name FROM slm_sections WHERE school_id = @SCHOOLID ORDER BY section_name ASC"
            dbconnect()
            conn.Open()

            cmd = New MySqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@SCHOOLID", My.Settings.SchoolID)

            Dim adptr As New MySqlDataAdapter(cmd)
            Dim table As New DataTable()

            adptr.Fill(table)
            cb.DataSource = New BindingSource(table, Nothing)
            cb.DisplayMember = "section_name"
            cb.ValueMember = "ID"

            cmd.Dispose()
            adptr.Dispose()
            conn.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

In passing values from datagridview to the Combobox1, here is my code

            Dim frm2 As New FormAddNewStudent
            With frm2

                frm2.Text = "Update Student Info"

                LoadGradeLevels(.CbGradeLevel)
                LoadSections(.CbSection)
                LoadStrands(.CbStrand)

                .TxtLastName.Text = StudentList.SelectedCells(5).Value.ToString
                .TxtFirstName.Text = StudentList.SelectedCells(6).Value.ToString
                .TxtMiddleName.Text = StudentList.SelectedCells(7).Value.ToString

                .TxtLRN.Text = StudentList.SelectedCells(8).Value.ToString

                .CbGender.SelectedItem = StudentList.SelectedCells(9).Value.ToString
                .CbGradeLevel.SelectedItem = StudentList.SelectedCells(10).Value.ToString
                .CbSection.SelectedItem = StudentList.SelectedCells(11).Value.ToString
                .CbStrand.SelectedItem = StudentList.SelectedCells(12).Value.ToString
                .BtnSave.Text = "Update"



            End With
            frm2.ShowDialog()

The Studentlist is the Datagridview, the StudentList.SelectedCells(1).Value.ToString is where the data that I need to be loaded into the Combobox1.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source