Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

Is it possible to insert a string item into a combobox which is being populated from a table?

My code is as follows:
VB
dim dv as dataview
dv = obj.get()
ddlReasons.DataSource = dv.Table
ddlReasons.DisplayMember = dv.Table.Columns("Reason").ToString
ddlReasons.ValueMember = dv.Table.Columns("Reason").ToString


I tried putting in
ddlReasons.Items.Insert(0,"--Please Select--")


but still the combobox only displays the items it retrieved from the table.

Thanks.
Posted
Updated 2-Jun-11 21:19pm
v2
Comments
Karwa_Vivek 3-Jun-11 3:04am    
Please Mark Your Solution .It will Help Others Too.
Green Button.
have fun !
Dalek Dave 3-Jun-11 3:19am    
Edited for Grammar and Readability.

Or Something Like This

ddlReasons.Items.Add("--Select---")

ddlReasons.DataSource = dv.Table
ddlReasons.DisplayMember = dv.Table.Columns("Reason").ToString
ddlReasons.ValueMember = dv.Table.Columns("Reason").ToString
 
Share this answer
 
v2
Try this
VB
dim dv as dataview
dv = obj.get()

      Dim dr As DataRow
      dr = dv.Table.NewRow()
      dr("ID") = 0
      dr("name") = "--Please Select--"
      dv.Table.Rows.InsertAt(dr, 0)
ddlReasons.DataSource = dv.Table
ddlReasons.DisplayMember = dv.Table.Columns("Reason").ToString
ddlReasons.ValueMember = dv.Table.Columns("Reason").ToString
 
Share this answer
 
v2
Comments
Dalek Dave 3-Jun-11 3:20am    
Edited for Code Block.
Whenever I need to do this, I do it at the query level.
It makes my code most consistent.
Say my query to populate the dropdown is
Select name, id from myTable

I add a union clause to that.
Select name, id from myTable
Union
Select '---Please Select---', 0
.
That usually works.
 
Share this answer
 
Comments
AnnSJ 3-Jun-11 3:07am    
thanks... this works fine!
Abhinav S 3-Jun-11 3:12am    
You are welcome.
Member 2393256 23-Mar-13 11:33am    
That's the way to do it!!! Thanks!!!

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