必须在Formload上保存vb.net组合框项目

时间:2014-03-01 07:04:03

标签: vb.net

Private Sub frmemployedetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try

        Dim dr As OleDbDataReader
        opendb()

        cmd.Connection = con
        cmd.CommandText = "Select DISTINCT department from employedetails"

        'Execte reader function is used to hold more than one value from the table
        dr = cmd.ExecuteReader()


        ' Fill a combo box with the datareader
        Do While dr.Read = True
            cbodepartment = dr.Item(0)
        Loop

        con.Close()

    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
    btnupdate.Enabled = False
    fill_grid()
    cbogender.Items.Add("Male")
    cbogender.Items.Add("Female")
End Sub

这是我的代码我是新进入VB.net的PLZ帮助我 所以我可以将项目添加到之前输入的Combobox中 表单加载时,它将显示我输入的所有不同项目

1 个答案:

答案 0 :(得分:1)

如果您想使用ComboBox数据库表中包含的部门列表填充employeedetails,可以试试这个:

Dim Connection As OleDb.OleDbConnection
Connection = New OleDb.OleDbConnection("YourConnectionString")

        Dim SQL As String = "SELECT DISTINCT department FROM employedetails"

        Try
            Connection.Open()

            Dim DA As New OleDb.OleDbDataAdapter(SQL, Connection)
            Dim DS As New DataSet("DS")
            DA.Fill(DS)

            Dim DT As DataTable
            DT = DS.Tables(0)

            For Each DR As DataRow In DT.Rows    
                cbodepartment.Items.Add(DR.Item("department").ToString)
            Next

            Connection.Close()

        Catch ex As Exception

            MsgBox(ex.Message)
            Connection.Close()

        End Try