469,267 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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

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!
Nov 16 '05 #1
3 10441
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[i]["ColumnName"];
li.DataValueField=dv.tables["table"].rows[i]["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" <ch*********@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
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!

Nov 16 '05 #2
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#" wrote:
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[i]["ColumnName"];
li.DataValueField=dv.tables["table"].rows[i]["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" <ch*********@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
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!


Nov 16 '05 #3
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#" wrote:
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[i]["ColumnName"];
li.DataValueField=dv.tables["table"].rows[i]["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" <ch*********@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
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!


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.