How to: Add "ListItems" to DropDownLists that are binded

G

Guest

I have binded a dropdownlist control to a data view. I am trying to add a
listitem to this already binded control, however, i cannot figure this out.
My code is:

// code to create data view object (dv)

DropDownList1.DataSource = dv;
DropDownList1.DataMember = "vStA";
DropDownList1.DataTextField = "vStAText";
DropDownList1.DataValueField = "vStAId";
if (bNewQuestion)
{
DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
DropDownList1.SelectedIndex = 0;
}

Is it simply not possible to add ListItems to already binded controls? Or is
it my code?

Thanks!
 
E

ED_C#

You might want to try something like this

for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
{
ListItem li=new ListItem();
li.DataTextField=dv.tables["table"].rows["ColumnName"];
li.DataValueField=dv.tables["table"].rows["ColumnName"];
DropDownList1.items.add(li);
}

This method doesn't bind the data to the control and you should be able to
add values later using the same method.

Good Luck.
 
G

Guest

For some reason this is not working either. I have this code inserted w/in a
switch block, which appears to be causing the problem... although i cannot
see how.

Using VS.NET, the intellisense doesn't automatically recognize the ListItem
when instatiated with the "new" key word. However, when i create the ListItem
"outside" the switch block, intellisense automatically recognizes the
ListItem object immediately right typing the "new" constructor to instantiate
it.

I know this makes no sense. Maybe it's a bug w/ my VS.NET?

ED_C# said:
You might want to try something like this

for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
{
ListItem li=new ListItem();
li.DataTextField=dv.tables["table"].rows["ColumnName"];
li.DataValueField=dv.tables["table"].rows["ColumnName"];
DropDownList1.items.add(li);
}

This method doesn't bind the data to the control and you should be able to
add values later using the same method.

Good Luck.


charliewest said:
I have binded a dropdownlist control to a data view. I am trying to add a
listitem to this already binded control, however, i cannot figure this
out.
My code is:

// code to create data view object (dv)

DropDownList1.DataSource = dv;
DropDownList1.DataMember = "vStA";
DropDownList1.DataTextField = "vStAText";
DropDownList1.DataValueField = "vStAId";
if (bNewQuestion)
{
DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
DropDownList1.SelectedIndex = 0;
}

Is it simply not possible to add ListItems to already binded controls? Or
is
it my code?

Thanks!
 
G

Guest

FYI:

I was able to make my code work (the original code) by creating and
inserting the new ListItem "AFTER" calling the DataBind() method on the drop
down list control.

ED_C# said:
You might want to try something like this

for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
{
ListItem li=new ListItem();
li.DataTextField=dv.tables["table"].rows["ColumnName"];
li.DataValueField=dv.tables["table"].rows["ColumnName"];
DropDownList1.items.add(li);
}

This method doesn't bind the data to the control and you should be able to
add values later using the same method.

Good Luck.


charliewest said:
I have binded a dropdownlist control to a data view. I am trying to add a
listitem to this already binded control, however, i cannot figure this
out.
My code is:

// code to create data view object (dv)

DropDownList1.DataSource = dv;
DropDownList1.DataMember = "vStA";
DropDownList1.DataTextField = "vStAText";
DropDownList1.DataValueField = "vStAId";
if (bNewQuestion)
{
DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
DropDownList1.SelectedIndex = 0;
}

Is it simply not possible to add ListItems to already binded controls? Or
is
it my code?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top