如何在 VB.NET 中填充组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28132972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:07:57  来源:igfitidea点击:

HOW TO FILL COMBO BOX IN VB.NET

vb.net

提问by Kemmy

I have a code to fill my combo box but every time i close the form the list is being doubled, if i have a list from my database of English, Mathematics, Science after i close the form and open it again the list showing is now English, Mathematics, Science, English, Mathematics, Science

我有一个代码来填充我的组合框,但是每次我关闭表单时,列表都会加倍,如果我在关闭表单并再次打开它后我的英语、数学、科学数据库中有一个列表,则显示的列表现在是英语、数学、科学、英语、数学、科学

HERE IS THE CODE,

这是代码,

    Call OpenDB()

    cmd.CommandText = "select * from Subject"
    cmd.Connection = conn
    dr = cmd.ExecuteReader

    While dr.Read()
        cmbSubject.Items.Add(dr("Subject Name"))    

    End While
    dr.Close()

    Call CloseDB()

回答by

The problem is not in the method you are using to bind the combobox. in each time it binds that's why you are getting duplicate records in the database. to avoid this please clear the comboboxbefore each bind, like the following:

问题不在于您用来绑定combobox. 在每次绑定时,这就是您在数据库中获得重复记录的原因。为避免这种情况,请combobox在每次绑定之前清除,如下所示:

Call OpenDB()
cmbSubject.Items.Clear ' extra statement added to clear the item collection
cmd.CommandText = "select * from Subject"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read()
    cmbSubject.Items.Add(dr("Subject_Name"))
End While
dr.Close()
Call CloseDB()

If you need an alternate method for binding the comboboxi will suggest you binding with Datasetfollowing is the example code for this :

如果您需要另一种绑定方法,combobox我建议您使用Dataset以下示例代码进行绑定:

    Dim  SQL As String= "select Subject_Name,Subject_code from Subject"
    Dim adapter As New OdbcDataAdapter(SQL, conn) '<-- This function will Retrieve Data and Return as Dataset together with table name
    Dim myData As New DataSet
    myData.Fill(lobjDataSet, tblName)
    cmbSubject.DataSource = ds_myData 
    cmbSubject.DataMember = "Subject"
    cmbSubject.DataTextField = "Subject_Name"
    cmbSubject.DataValueField = "Subject_code"
    cmbSubject.DataBind()
    myData .Dispose()

回答by Rino

Public Class frmRegistration
    Dim obj As New Entity.Registration
    Dim bus As New Business.Registration



    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        BindingSource1.EndEdit()
        bus.Save(obj)
        RefreshGrid()
        MsgBox("Saved.")
    End Sub


    Sub RefreshGrid()
        Dim dt1 As DataTable = bus.Select()
        BindingSource2.DataSource = dt1
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.DataSource = BindingSource2
    End Sub

    Private Sub Registration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshGrid()
        NewItem()
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        NewItem()
    End Sub

    Sub ResetDis()
        BindingSource1.DataSource = obj
    End Sub

    Sub NewItem()
        obj = New Entity.Registration
        ResetDis()
    End Sub


    Private Sub BindingSource2_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource2.CurrentChanged
        obj = New Entity.Registration(CType(Me.BindingSource2.Current, DataRowView).Row)
        ResetDis()
    End Sub


End Class