Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first choice in combo box when using a dataset

Status
Not open for further replies.

michelleHEC

Programmer
Oct 20, 2003
95
US
I have a combo box that gets filled from a dataset with account numbers. The problem I am having is that the first account in the dataset is being used by accident by the user. (they are forgetting to choose the account) I would like to have the first thing on the list be "Choose your account" and then the rest of the items be from the dataset. Is that possible and if not, does anyone have any other ideas?
Michelle
 
How are you creating the dataset? You may be able to add "Choose your account" as the first row in the dataset. I've done this before in stored procedures.
Andrea
 
Hi,

Here is what you need to do

'declare a new variable(row)
dim row as datarow

'after you generate a ds(dataset) you will need to add
'a new row

row = ds.tables(0).newrow
row.item(0) = "Choose your account"
ds.tables(0).row.add(row)

Then populate the dropdown box

'populate dropdown box
For Each row In ds.Tables(0).Rows
ComboBox1.Items.Add(row(0))
Next

'select Item of choice
ComboBox1.Text = "Choose your account"


I think that should do it.
-mike


 

I am creating a dataadapter and fill a dataset and then binding it to the combo box.
 
also just a quick note, if you want it sorted you can do this.

'change
row.item(0) = "Choose your account"

'to this
row.item(0) = "0000" 'dummy value

after you populate the combobox sort it
combobox1.sorted = true

then replace the dummy value
combobox1.item(0) = "Choose your account"

this will make "choose your account to be the first item in the list
 


I am getting the error that one of my fields will not allow nulls.
michelle
 
maybe your datarow has 2 columns?
you could try this
'after you generate a ds(dataset) you will need to add
'a new row

row = ds.tables(0).newrow
row[0] = "Choose your account"
row[1] = "Test"

ds.tables(0).row.add(row)

 

ok, got pass that error but now it says "Cannot modify the items collection when the DataSource property is set."
michelle
 
I believe you need to add the row to the dataset before binding it to the combobox.

Andrea
 

i was just trying that. i was binding it in the property box but know i am trying it in code to see if that will do it
 
You could change your stored proc such that when it returns the dataset to the db layer you can add this chooseyouraccount as the first option
 
In your stored procedure, you can create a temp table to hold your combobox values. Insert the "choose account" into the temp table first, then insert the rest of your values. Then, just select everything from the temp table.

Andrea
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top