Binding in an ASP.NET DropDownList

If you need to bind two seperate database columns bound with the Value and Text properties of a DropDownList control, it can be done using the DataValueField and DataTextField properties of DropDownList.

I have a DataTable with two columns called - Name and IdCode. I wanted to display Name as the text of a DropDownList items but IdCode as value so it can be use for other purposes.

This is how you do it.

MyList.DataSource = ds.Tables(0)
MyList.DataValueField =
"IdCode"
MyList.DataTextField = "Name"

Ebook Download
View all
Learn
View all