Reputation: 54133
I am trying to do this:
protected void Page_Load(object sender, EventArgs e)
{
Group g = SecurityManager.GetGroup("Programmers");
IEnumerable<EmployeGroup> emps =
SecurityManager.GetEmployeesByGroup(g.GroupID);
ListBox1.DataSource = emps;
ListBox1.DataTextField = "Employee.EmployeName";
ListBox1.DataBind();
}
Inside an EmployeGroup
there is an Employee
and a Group.
I want to show the Employee's EmployeName
member.
Is there some way to do this without doing: for each Employee. Add to listbox.
Upvotes: 0
Views: 57
Reputation: 2662
You have a few choices. You can either extend the Employee class and introduce a new property called EmployeeNameAndId
public string EmployeeNameAndId
{
get
{
return String.Format("{0} ({1})", EmployeeName, EmployeeId);
}
}
Another option is overriding the OnDataBinding event and concatenate the two properties there. I don't recommend this; it's lousy encapsulation among other reasons, but I wanted you to be aware of the option.
The final option is to create an intermediate data source from your query and bind to that:
var employees = SecurityManager.GetEmployeesByGroup(g.GroupID).Select(e => new { EmployeeNameAndId = e.EmployeeName + " (" + e.EmployeeId + ")"});
ListBox1.DataSource = employees ;
ListBox1.DataTextField = "EmployeeNameAndId";
ListBox1.DataBind();
Upvotes: 1