Reputation:
This is my list definition
public class EventsList
{
public int EventID { get; set; }
public string EventName { get; set; }
}
This is C# code
string strCurrentUser = CommonWeb.GetLoginUser();
EventsClass EventObj = new EventsClass();
DataSet ds;
List< EventsList> eventList = new List<EventsList>();
EventsList eventobj = new EventsList();
ds=EventObj.GetEvents(strCurrentUser);
I have a dropdown in which it should have the dropdown menu as the EventName alone. How can I retrieve only the EventName from the list and display that in the dropdown?
Upvotes: 1
Views: 4733
Reputation: 79929
To display the EventName
only to the dropdown, you will need to set the following two properties:
DataValueField
to EventId
.DataTextField
to EventName
.Something like:
dropdown.DataSource = ds;
dropdown.DataValueField = "EventID";
dropdown.DataTextField = "EventName";
dropdown.DataBind();
Upvotes: 4
Reputation: 2174
You can use like also if you have tables in datasource
DropDownlist2.DataSource = dataSet.Tables["Table2"];
Dropdownlist2.DataTextField= "SomeTextFieldInTable2";
Dropdownlist2.DataValueField="SomeValueFieldInTable2";
DropDownlist2.DataBind();
Upvotes: 0
Reputation: 3268
In asp.net, you can achieve this like
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "EventName";
DropDownList1.DataValueField = "EventID";
DropDownList1.DataBind();
Upvotes: 0
Reputation: 1457
ddlName.DataSource = ds;
ddlName.DataTextField= "EventName";
ddlName.DataValueField= "EventID";
ddlName.DataBind();
Here DataValueField is upto you whether to use ID or the Name itself.
Upvotes: 0