adding data to a dropdownlist already containing items

Lemon

Member
Joined
Jul 1, 2005
Messages
12
Programming Experience
1-3
Well, I'm hoping someone can help me....

I've got a web application that queries data from a database based on selections made in previous dropdownlists. The problem is that some of the final queries return only one data item.

I'm using the onSelectedIndexChanged with autopostback enabled as my events in the lists. And thats the problem, if theres only one item in the dropdownlist, its imposible to change the index, thus I've got no event firing the next block of code I need executed...

If anyone has any idea on how to counter this problem, please help!
I thought if it were possible to add data to already manually inputted items already in the list, that'd be great, so that there's already 1 item in there before the other items are added... So that if only one item is retrieved by the from the database, there will be at least two items in there, allowing a change in the selected index...

Just how to do that though, I have no idea!
 

Gambit_NI

Active member
Joined
Jun 16, 2005
Messages
42
Location
Belfast
Programming Experience
3-5
can you not put a blank item into the returned dataset/datatable before setting the datasource of the lists?


if you do this you may find the selectedindexchanged event fires, but you just need to set a boolean or similar in the event so that it doesnt fire on populating

here's a snippet of code i used to populate a ddl

'Add empty row to datatable at index 0

Dim row As DataRow = dst_DataSet.Tables(0).NewRow()
row(0) = -1
' Numeric Id column
row(1) = "" 'Text column
dst_DataSet.Tables(0).Rows.InsertAt(row, 0)
dst_DataSet.AcceptChanges()

'Bind the ddl

bln_CategoryRunOnce =
True'set to true so that selected index changed code does not fire
cbo_Category.Items.Clear()
cbo_Category.DataSource = dst_DataSet.Tables(0)
cbo_Category.ValueMember = "ID"
cbo_Category.DisplayMember = "Description"
'//Set focus to combo
cbo_Category.Focus()
bln_CategoryRunOnce =
False'set to false so that selected index changed code can now fire

then the 1st line in the SelectedIndexChanged event reads

If bln_CategoryRunOnce = TrueThenExitSub


HTH,
Craig

 

Lemon

Member
Joined
Jul 1, 2005
Messages
12
Programming Experience
1-3
Thanx alot Craig, that definitely helped! Its just that little thinking out of the box that gets me sometimes... But, yeah its working great! :D
 

Bigbeanpole

Member
Joined
Jul 7, 2005
Messages
15
Programming Experience
1-3
This is almost exactly what I'm trying to do with a windows application I'm coding right now, however, the supplied code isn't working for my instance.

In my scenario, I've got a dropdown populated from a database with 8 rows being returned. Ok so far. The SelectedIndex of the combo box on form load is <> 0, yet it still shows a SelectedValue (making it look like that's the selected value, even though it really isn't). To get around that, I would like have the combo box start on a blank row. Without adding to the database a blank line, is it possible to add a blank line to the datatable after having it populated by a database query?


I've tried a few methods, and it continually gives me errors of varying degrees of blowing up my application. :( Help is greatly appreciated!
 

Gambit_NI

Active member
Joined
Jun 16, 2005
Messages
42
Location
Belfast
Programming Experience
3-5
in the code above, its assumed the datatable is already populated, it then adds the empty record, into the already populated datatable, at index 0
 

Bird

New member
Joined
Aug 10, 2005
Messages
1
Programming Experience
5-10
Use this code to add any number of items to your dropdownlist after you have binded to it:

mydropdownlistID.Items.Insert(0, New ListItem("Select One", 0))

The first argument is the selectedIndex, the second is the Text of the ListItem and the Value of the ListItem.

This should allow you to have an additional item in your dropdown and insolve your problem.

Lemon said:
Well, I'm hoping someone can help me....

I've got a web application that queries data from a database based on selections made in previous dropdownlists. The problem is that some of the final queries return only one data item.

I'm using the onSelectedIndexChanged with autopostback enabled as my events in the lists. And thats the problem, if theres only one item in the dropdownlist, its imposible to change the index, thus I've got no event firing the next block of code I need executed...

If anyone has any idea on how to counter this problem, please help!
I thought if it were possible to add data to already manually inputted items already in the list, that'd be great, so that there's already 1 item in there before the other items are added... So that if only one item is retrieved by the from the database, there will be at least two items in there, allowing a change in the selected index...

Just how to do that though, I have no idea!
 
Top Bottom