473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 10582
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Assimalyst | last post by:
Hi, I'm attempting to use a data reader to load data from a single table, tblCountry, with two columns, countryNo (the Key) and countryName, into a DropDownList box. Here's the code: ...
0
by: Dinesh Jain | last post by:
Hi all, To give functionality like cut-paste, I used listview1.listitems.item(0).ghosted=true in VB. But what is it's equivalent in VB.NET ? Please help, Thanks in advance, -Regards,
1
by: Klaas | last post by:
I have a dropdownlist. When the user clicks on a button I want the selected listitems text to be line-through. Is this possible in IE? or with some javascript? tia
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.