Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

C# - Populate a dropdown including selected index based on results of query

Hey there.

I have something of a dilemma. I am pulling a list of names from a database (MySQL query) and putting them into a dropdown box.

I am able to set the text and value field to the first values out of the query, but I cannot set the selected index as a specific row.

The setup is thus:

A datatable contains the information pulled from the query and is then taken by a dropdown and the values are filled like this:


DataTable DT declared earlier.

DropDownList drop = (DropDownList)e.Item.FindControl("ddListAllocations");
drop.DataSource = DT;
drop.DataValueField = "userid";
drop.DataTextField = "username";
drop.DataBind();

This is called from an OnItemDataBound method in an ASP:Repeater control.

There are two tables in use here:

tblOrders
tblUsers

tblOrders contains two fields I need:
orderNo
orderAgent

tblUsers contains two fields I need:
userID
userName


Is there a way to bind the orderAgent and userid together to create a selected index for each orderno?

Thanks in advance.
0
DanBAtkinson
Asked:
DanBAtkinson
  • 2
1 Solution
 
harshitsCommented:
Hi,

I am not much sure what kind of functionality are you trying to implement. I get that you have two drop down list. What is the dependency between these two drop downs?

The syntax for selecting a particular index is DropDown.SelectedIndex= [index]; after the drop down is bound.

It will be helpful if you clarify a bit more

Harshit Sheth


0
 
DanBAtkinsonAuthor Commented:
There is only one dropdown list which is bound to a repeater. Therefore, the set number of dropdown boxes is dynamic.
0
 
DanBAtkinsonAuthor Commented:
Na. I was waiting for an answer.

I figured it out in the end.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now