Click here to Skip to main content
15,885,129 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Out of no help in Google, I came here to ask this.

I have a table named Agent with columns AgentId and AgentName in Sql Server DB. Using Visual Studio 2008(VB.NET), I am developing a windows based app. I have a ComboBox control in a windows form. I need this ComboBox control to be filled with the Agent Table. I used the following code, but still am not able to set "Select Agent" as the default selected value.

VB
ComboBox.DataSource = DataSet1.Tables(0)
ComboBox.DisplayMember = "AgentName"
ComboBox.ValueMember = "AgentId"


When I add "Select Agent" using,
VB
ComboBox.Items.Add("Select Agent")
, I get the Error, "Items collection cannot be modified when the DataSource property is set."

Any help is highly appreciated. Thanks!
Posted
Updated 31-Oct-13 1:11am
v2

try below:
C#
ComboBox.Items.Add("Select Agent")
ComboBox.DataSource = DataSet1.Tables(0)
ComboBox.DisplayMember = "AgentName"
ComboBox.ValueMember = "AgentId"
ComboBox.DataBind() // Add This Line into Your Code

ComboBox.SelectedValue = "Select Agent"
 
Share this answer
 
Comments
arunrk87 11-Nov-13 3:17am    
I dont get the DataBind() keyword. Because it is VB.NET?
prashant patil 4987 12-Nov-13 5:54am    
ok lets refer below link
http://vb.net-informations.com/dataset/bind-combobox.htm
arunrk87 18-Nov-13 7:57am    
but Prashant, ders no idea about DataBind() or default ComboBox value in the link you posted
 
Share this answer
 
Comments
arunrk87 18-Nov-13 8:05am    
no dis dint help out...
Got it! check the link below

http://stackoverflow.com/a/14868976/194276
 
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