本文介绍了我如何填充datagridview组合框单元mysql VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试从mysql数据库填充datagridview组合框。情况是,每个科目都有教师,可以在成功加载科目后选择。我没有运气。任何人都可以帮我在datagridviewcell中按主题加载教师。在此先感谢。



我尝试过:



Hello, I am trying here to populate a datagridview comboboxcell from mysql database. The situation is, each subject will have teachers that can be selected after the subjects has been successfully loaded. I have no luck. Can anyone help me to load teachers by subject in datagridviewcell. Thanks in advance.

What I have tried:

Try
            Dim cmb As New DataGridViewComboBoxColumn
            cmb.HeaderText = "Teacher"
            cmb.Name = "cmb"
            dgvsubj.Columns.Add(cmb)

            Dim item As DataGridViewRow
            For Each item In dgvsubj.Rows

                Dim dtgCol As New DataGridViewComboBoxCell

                str = "SELECT teacher_subjects.TeacherID, teacher.Name, teacher_subjects.SubjectID" _
                & " FROM(teacher, teacher_subjects)  WHERE teacher.TeacherID = teacher_subjects.TeacherID " _
                & " AND teacher_subjects.SubjectID =" & item.Cells("SubjectID").Value

                Dim sqlcmd As New MySqlCommand(str, con)
                If con.State = ConnectionState.Closed Then con.Open()
                Dim reader = sqlcmd.ExecuteReader

                If reader.HasRows Then

                    While reader.Read
                        dtgCol = item.Cells(4)
                        dtgCol.DisplayMember = reader.Item("TeacherID")
                        dtgCol.ValueMember = reader.Item("Name")
                    End While
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            con.Close()
        End Try

推荐答案


这篇关于我如何填充datagridview组合框单元mysql VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:43