且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不删除数据源的情况下删除组合框数据绑定

更新时间:2023-10-19 19:27:34

尝试一下,

Try this,

comboboxid.Items.Clear()




希望对您有帮助.


如果要在基本组合框的OnSelectedChanged事件中删除子组合框的项目,


将此代码放在基本组合框的OnSelectedChanged事件中.




Hope it helps..


If you want to remove the items of child combobox in OnSelectedChanged event of base combobox means,


put this code in OnSelectedChanged event of base combobox.


Private Sub Bindall()
       Try
           NameCand.DataSource = Nothing
           Contact.DataSource = Nothing
           Contact1.DataSource = Nothing
           ResNumber.DataSource = Nothing
           Age.DataSource = Nothing
           Location.DataSource = Nothing
           Qualification.DataSource = Nothing
           CurentOrg.DataSource = Nothing
           WorkExp.DataSource = Nothing
           CalledBy.DataSource = Nothing

           Dim a1 As New SqlDataAdapter("select distinct Name from TotTab", My.Settings.Equation1ConnectionString)
           Dim a2 As New SqlDataAdapter("select distinct Contact from TotTab", My.Settings.Equation1ConnectionString)
           Dim a3 As New SqlDataAdapter("select distinct Contact1 from TotTab", My.Settings.Equation1ConnectionString)
           Dim a4 As New SqlDataAdapter("select distinct ResNumber from TotTab", My.Settings.Equation1ConnectionString)
           Dim a5 As New SqlDataAdapter("select distinct Age from TotTab", My.Settings.Equation1ConnectionString)
           Dim b1 As New SqlDataAdapter("select distinct Location from TotTab", My.Settings.Equation1ConnectionString)
           Dim b2 As New SqlDataAdapter("select distinct Qualification from TotTab", My.Settings.Equation1ConnectionString)
           Dim b3 As New SqlDataAdapter("select distinct CurrentOrganizatn from TotTab", My.Settings.Equation1ConnectionString)
           Dim b4 As New SqlDataAdapter("select distinct WorkExp from TotTab", My.Settings.Equation1ConnectionString)
           Dim b5 As New SqlDataAdapter("select distinct CalledBy from TotTab", My.Settings.Equation1ConnectionString)


           Dim ds As New DataSet

           a1.Fill(ds, "Name")
           a2.Fill(ds, "Contact")
           a3.Fill(ds, "Contact1")
           a4.Fill(ds, "ResNumber")
           a5.Fill(ds, "Age")
           b1.Fill(ds, "Location")
           b2.Fill(ds, "Qualification")
           b3.Fill(ds, "CurrentOrganizatn")
           b4.Fill(ds, "WorkExp")
           b5.Fill(ds, "CalledBy")

           SetLookupBinding(NameCand, ds.Tables("Name"), "Name")
           SetLookupBinding(Contact, ds.Tables("Contact"), "Contact")
           SetLookupBinding(Contact1, ds.Tables("Contact1"), "Contact1")
           SetLookupBinding(ResNumber, ds.Tables("ResNumber"), "ResNumber")
           SetLookupBinding(Age, ds.Tables("Age"), "Age")
           SetLookupBinding(Location, ds.Tables("Location"), "Location")
           SetLookupBinding(Qualification, ds.Tables("Qualification"), "Qualification")
           SetLookupBinding(CurentOrg, ds.Tables("CurrentOrganizatn"), "CurrentOrganizatn")
           SetLookupBinding(WorkExp, ds.Tables("WorkExp"), "WorkExp")
           SetLookupBinding(CalledBy, ds.Tables("CalledBy"), "CalledBy")



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

Public Shared Sub SetLookupBinding(ByVal toBind As ComboBox,
ByVal dataSource As Object, ByVal displayMember As String)
       toBind.DataBindings.Clear()
       toBind.DisplayMember = displayMember
       'toBind.ValueMember = valueMember

       '// Only after the following line will the listbox

       '// receive events due to binding.

       toBind.DataSource = dataSource
       toBind.SelectedIndex = -1
   End Sub



我在窗体加载时调用了bindall方法.
我不知道这是否是正确的方法,但是我得到了我所需要的.
同样通过这种方式,我只获得唯一的记录,并且每个组合框都是独立的.
谢谢大家的辛苦.



I have Called bindall method on form load.
I don''t know whether it''s the right approach but I got what I needed.
Also in this way I get only unique records and every combobox is independent.
Thank you every1 for the amount of time that you guys put into this.


如果我理解正确,您想将此组合绑定到选项列表,但实际上并没有希望选择是数据绑定的(至少不绑定到数据库).这是正确的吗?

如果是这样,则有两个合理的选择.放弃数据绑定并手动构建组合的项目列表:

If I understand correctly, you want to bind this combo to a list of options, but you don''t actually want the selection to be data bound (at least not to the database). Is this correct?

If so, there are two reasonable options. Either ditch data binding and construct the combo''s item list manually:

void Populate(ListControl list, DataTable source){
 // Combos are ListControls, so are some other things you might want to populate like this
 list.Items.Clear();
 foreach(DataRow row in source) list.Items.Add(row[0]);
}



...或绑定到表的副本,这应该防止所选索引在控件之间共享(只要为每个副本创建一个副本即可).



... or bind to a copy of the table, which should prevent the selected index from being shared between controls (as long as you make a copy for each one).