Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz help me,

what i need is drop down binding with arraylist or list
with value field and text field

ids Name
---- -----
1 Car
2 Bus
3 XYZ

in this way i have number of fields. help me in doing this
public int ids
{
get;
set;
}
public string Name
{
get;
set;
}
Posted
Updated 3-Mar-12 4:18am
v2
Comments
[no name] 3-Mar-12 10:39am    
What have you tried so far?
Yaseer Arafat 4-Mar-12 4:47am    
What do you want to know?You need a list to feel a dropdownlist.It may be a arraylist,Dictionary or Generic list.Make a list and feel it up.

I assume we have a drop down list (DropDownList1) one the page with following structure,

ASP.NET
<asp:dropdownlist id="DropDownList1" runat="server" datatextfield="Name" datavaluefield="Id" xmlns:asp="#unknown">
</asp:dropdownlist>


and related code behind to populate the DropDownList with data,

C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IList persons = new List<Person>()
        {
             new Person(){ Id="1", Name="A"},
             new Person(){ Id="2", Name="B"},
             new Person(){ Id="3", Name="C"},
        };

        DropDownList1.DataSource = persons;
        DropDownList1.DataBind();
    }

    public class Person
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}


So from the above code we can seer, I created a Person type with two property Id and Name. I set the Id as value field for the DropDownList and Name as Textfield.

I Created a List of Person and set that list as data source of the DropDownList. Finally to Bind the data source with the DropDownList using DataBind().

Note: I have done all these on a page named Default.aspx

Hope it helps :)
 
Share this answer
 
C#
Dictionary<int,string> list=new Dictionary<int,string>();
list.Add(1," Car");
list.Add(2," Bus");
list.Add(3," XYZ");
dropItems.DataSource=list.toList();

or for your problem We need Create List of Vehicle Class as
C#
public class Vehicle
{
 public int ids  {  get;set;}
 public string Name{get;set;}
}

Then load it ...........................
C#
List<vehicle> Vehicles=new List<vehicle>;
Vehicles.Add(1," Bus");
Vehicles.Add(2," Car");
Vehicles.Add(3," XYZ");
dropItems.DataSource=Vehicles;

or you can get it from Database from Vehicle Table or something.
I think you have your solution.
 
Share this answer
 
v2
if you get the values from a db query
then you can make...


C#
var result = RESULT_OF_YOUR_QUERY;
dropItems.DataSource=result;
dropItems.DataValueField = "ids";
dropItems.DataTextField = "Name";
dropItems.DataBind();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900