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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Populating a ComboBox 1

Status
Not open for further replies.

davecapone

Programmer
Oct 3, 2000
48
0
0
US
Hi,

I am using the following C# code to populate a DropDownList Box named Category.

foreach (DataRow row in ds.Tables[0].Rows) {
ListItem currentItem=new ListItem();
currentItem.Text=row[0].ToString();
currentItem.Value=row[1].ToString();
CategoryList.Add(currentItem);
}
Category.DataSource=CategoryList;
DataBind();

Problem is for whatever reason, when it is binded to the DataSource, the Text and Value properties in the HTML are the same. I know the data in the DataTable is properly because if I switch the 0 and 1 in the row subscript, the data properly changes. But for whatever reason, whatever is set for the Text field also becomes the value field.

Any suggestions?
 
You're doing the work twice.
If you add the rows one at a time, you don't need to databind the control in order to populate it.

The properties for databinding are:

DataTextField : the name of the field in the dataset to use for the "Text" of the listitem

DataValueField : the name of the field in the dataset to use for the "Value" of the listitem


Hope this helps,
Elijah
 
Hi aceoft,

Totally forgot about being about to Bind the information that way. Made the change and it worked perfectly.

However, despite that way being much simpler and more efficient, shouldn't the other code work correctly. CategoryList is actually of type ListItemCollection, which contains ListItems, who has 3 basic properties, Text, Value, and Selected from what I remember. So I am a bit puzzled as to why that didn't work.
 
On a Side note, I might still prefer using a ListItemCollection as I'd like to have a "Select a Category" option as a header to the dropdownlist and I'm pretty sure there is no way to do this if it is databound directly to a database query. The only other thing I was thinking is I could use the Insert method of the DataRow class to add a new record to the DataTable after the Query.
 
Ok I see where you're coming from now. I have only used databinding with datasets so I didn't recognize the method you were using there with the categorylist object.

No matter what the datasource, though, the DataTextField and DataValueField must be set each to something different in the datasource for them to be different after databinding.

You can actually skip the first bit and just bind straight to the dataset with the DTF and DVF set to your fields in the dataset and all should be well. Then you can use the .Insert(0,theItem) method to add your "Please select" at the top.

Elijah
 
Hi aceoft,

In what Namespace/Class is the insert method, I have this infor last nigth and I can't find it again in MSDN. Does the insert work on the DropDownList or on the DataTable?
 
Sorry, the Insert method is a member of the Items collection in the ListBox class.

There are two overloads...
Code:
ListBoxName.Items.Insert(index,ItemText)
                 .Insert(index,ListItem)

Elijah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top