Question : Binding List<Class> to DropDownList C#

Hello,

See the code snippet of what we are trying to do below.  

Our List<TeamsSportsInfo> will bind just fine to the DropDownList using the directly exposed properties in TeamsSportsInfo like Key and ID.  But we need to assign the TeamsSportsInfo.DisplayNameSportsInfo.FullName property as the text field name for the DropDownList.

Does anyone know how this can be accomplished without modifying the schema classes (TeamsSportsInfo and DisplayNameSportsInfo)?  

We were thinking we could possibly do it manually using an OnDataBinding event method but the parameter data types for this are Object sender and EventArgs e in which "e" does not contain any "item" reference like other controls (i.e. repeater) which would hold the datasource data.

Does anyone know if using this event method would work and more importantly, how this would be done?

Any other ideas to accomplish this?

(and no, putting DisplayNameSportsInfo.FullName in the DataTextField property does not work)

Thanks in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
List<TeamsSportsInfo> listTeamsSportsInfo = (returns list)

ddlTeams.DataSource = listTeamsSportsInfo;
ddlTeams.DataTextField = "Key";
ddlTeams.DataValueField = "ID";
ddlTeams.DataBind();

//(the above code works just fine but we need the DataTextField to be DisplayNamesSportsInfo.FullName, any ideas?)


public class TeamsSportsInfo
{
  public Int32  Id{ get; set; }     
  public String Key (get; set; }    
  public Int32  PublisherId { get; set; } 
  public Int32  HomeSiteId { get; set; }
  public String HomeSiteName { get; set; } 
  public DisplayNamesSportsInfo DisplayNamesSportsInfo { get; set; }
}

public class DisplayNamesSportsInfo
{
  public Int32 Id { get; set; }
  public String FullName { get; set; }
}

Answer : Binding List<Class> to DropDownList C#

This one worked for me.

Arn
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
List<TeamsSportsInfo> teamsSportsInfos = GetList();

ddList.DataSource = (from obj in teamsSportsInfos
                   select new
                   {
                       Id = obj.Id,
                       FullName = obj.DisplayNamesSportsInfo.FullName
                   }).ToList();
ddList.DataValueField = "Id";
ddList.DataTextField = "FullName";
ddList.DataBind();
Random Solutions  
 
programming4us programming4us