VB.net组合框重复数据 [英] VB.net Combo Box duplicate data

查看:283
本文介绍了VB.net组合框重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个combobox.text填充后,我正在填充一个combo box.每次用户选择any选项时,第一次combobox.items列表都会正确填充.如果用户在第一个框中的项目之间移动,则第二个组合框不会清除,因此我会获得相同选项的重复条目.我需要帮助来清除重复的数据或在更改后清除第二个框.下面是我的代码,用于在填充第一个框之后填充第二个框.提前谢谢.

I am filling in a combo box after an another combobox.text is populated. Every time the user selects the any option the first time combobox.items list populates correctly. If the user goes between items in the first box the second combobox doesnt clear so I get duplicate entries for the same option. I need help clearing the duplicate data or having the Second box clear upon change. Below is my code for populating the second box after the first one is populated. Thanks in advance.

Private Sub cmbDevice_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbDevice.SelectedIndexChanged

Dim StrQ As String = "Select [Model] from " & cmbDevice.Text & " Group By [Model] "
        Dim da As New SqlDataAdapter(StrQ, con)
        If cmbDevice.Text = "*" Then
            cmdType.Text = ""
        Else
            da.Fill(ds, cmbDevice.Text)
            With Me.cmdType
                .DataSource = ds.Tables(cmbDevice.Text)
                .DisplayMember = "Model"
                .SelectedIndex = 0
            End With
        End If

推荐答案

为什么不先清除combobox.Items集合,然后再重新填充它.或者,您可以将DataSource属性设置为Nothing.
Why don''t you just clear the combobox.Items collection before populating it again. Alternatively, you could set the DataSource property to Nothing.


尝试在清除项目之前清除cmbType的数据源.
Try to clear DataSource of cmbType before Clearing Items.
Private Sub cmbDevice_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbDevice.SelectedIndexChanged
 
Dim StrQ As String = "Select [Model] from " & cmbDevice.Text & " Group By [Model] "
        Dim da As New SqlDataAdapter(StrQ, con)
        If cmbDevice.Text = "*" Then
            cmdType.Text = ""
        Else
            da.Fill(ds, cmbDevice.Text)
            cmdType.DataSource = "" ' Or you can set it as Nothing
            cmdType.Items.Clear()
            With Me.cmdType
                .DataSource = ds.Tables(cmbDevice.Text)
                .DisplayMember = "Model"
                .SelectedIndex = 0
            End With
        End If


这篇关于VB.net组合框重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆