Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a datasource from a list

Tags:

c#

Im iterating a list and getting hold of 2 fields "Name" and "Url"

i want to extract these fields and use them to create a datasource

I want to databind these to a dropdownList

DropDownList.DataTextField = "Name";
DropDownList.DataValueField = "Url";

how can I create a datasource based on this list? then do the above, then databind.

like image 865
raklos Avatar asked Aug 06 '26 16:08

raklos


1 Answers

Assuming that you have an list item named MyListItem and MyListItem has two properties Name and Url, you can bind a list of MyListItem like that :

List<MyListItem> dataSource = new List<MyListItem>();

MyListItem item1 = new MyListItem();
item1.Name = "Name 1";
item1.Url = "Url 1";
dataSource.Add(item1);

MyListItem item2 = new MyListItem();
item2.Name = "Name 2";
item2.Url = "Url 2";
dataSource.Add(item2);

dropDownList.DataSource = dataSource;
dropDownList.DataTextField = "Name";
dropDownList.DataValueField = "Url";
dropDownList.DataBind();
like image 93
Canavar Avatar answered Aug 11 '26 13:08

Canavar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!