MrM
MrM

Reputation: 21999

How do I bind a list to a ddl?

I have a list and I am trying to add the data to the ddl. It returns data, (namespace.List). But there is something I am missing... any suggestions?

public List<getBranch> Branch { get; private set; }
...                           
getBranch(user.code);
ddlOption.DataSource = Branch;                        
ddlOption.DataBind();

Upvotes: 0

Views: 232

Answers (1)

roman m
roman m

Reputation: 26551

All you're missing is to tell the dropdown what property of getBranch to show as text and what to use as value:

ddlOption.DataTextField = "propertyOfgetBranchToShowAsText";
ddlOption.DataValueField = "propertyOfgetBranchToUseAsValue";

ok, here's how the code should look like (i think):

your Branch class properties:

public int BranchValue {get;set;}
public string BranchText {get;set;}

...

public List<Branch> branchesToShow { get; private set; }
...                           
branchesToShow = getBranch(user.code); //get the list of branches
ddlOption.DataTextField = "BranchText"
ddlOption.DataValueField = "BranchValue";
ddlOption.DataSource = branchesToShow;                        
ddlOption.DataBind();

Upvotes: 5

Related Questions