本文介绍了如何在不删除数据源的情况下删除组合框数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visualstudio2010,正在vb.net中开发一个应用程序,后端是SQL2005

我有一个组合框,其中包含使用数据集从sql表中获取的项目

I am using Visualstudio2010,i am developing an application in vb.net and backend is SQL2005

I have a combobox with items that are taken from sql table using dataset

 <br />
comboboxname.Datasource=dataset.table("tablename")<br />
comboboxname.Displaymember="Columnname"<br />


我已经使用上面的代码绑定了组合框,我有5到6个组合框以相同的方式绑定.
由于绑定的原因,如果我从1个组合框中选择一个值,则会自动选择所有组合框的核心对应值.
我不希望自动选择所有值.
任何人都可以提出另一种方法或帮助我删除绑定.

我试过了:


I have used the above code to bind the combobox,I have 5 to 6 comboboxes which are binded in the same way.
Due to the binding,if I select a value from 1 combobox the coresponding values of all combobox gets selected automatically.
I don''t want all the values to be selected automatically.
Can any one suggest an alternate way or help me remove the binding.

I have tried:

 <br />
comboboxname.DataBindings.Clear()<br />


但这不起作用.

我只需要数据库表的列值即可在组合框中提出建议.





这是代码:


But it doesn''t work.

I just need the database table''s column values for giving suggestion in my combobox.





here is the code:

Private Sub Bindall()
        Try
Dim a As New SqlDataAdapter("select * from TotTab", My.Settings.Equation1ConnectionString)

              Dim ds As New DataSet
                a.Fill(ds,"tot")

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

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

    End Sub
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.

推荐答案

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.


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).


这篇关于如何在不删除数据源的情况下删除组合框数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:21
查看更多