Click here to Skip to main content
15,947,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 />

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.

I want to remove the binding and not the combobox items,so that I can access each combobox individually,without causing a change on other comboboxes.

please help its urgent.

here is the code:

VB
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


I have Called Bindall method on form load.
Posted
Updated 3-Jun-22 10:43am
v6
Comments
Shobana16 14-Dec-11 5:58am    
Whether all comboboxes are binded with the same ds(DB values)?.
Vinay Indoria 14-Dec-11 6:59am    
I am sorry for late reply..
Yes all comboboxes are binded to same db.
Anuj Banka 14-Dec-11 7:26am    
Post your code
Vinay Indoria 14-Dec-11 7:53am    
I have posted the code.
BobJanova 14-Dec-11 9:35am    
Putting 'urgent' in your message is likely to turn some people off answering it.

Try this,

VB
comboboxid.Items.Clear()



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.
 
Share this answer
 
Comments
Vinay Indoria 14-Dec-11 5:31am    
Thanks for your reply..
But i don't want to remove the child items,I want to remove the binding and not the combobox items,so that I can access each combobox individually,without causing a change on other comboboxes.
Shobana16 14-Dec-11 5:37am    
Whether all comboboxes are binded with the same ds(DB values)?
Vinay Indoria 14-Dec-11 6:29am    
Im sorry for late reply..
Yes all are binded to same db.
VB
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


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.
 
Share this answer
 
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).
 
Share this answer
 
I was able to break binding and preserve combobox source data by explicitly converting the datasource to a generic list:
combobox.datasource.ToList()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900