kruegerste
asked on
ASP.NET 3.5 Gridview DataBind to Generic Class Object List
Hello,
I'm trying to bind a List<> to a gridview but it isn't working because the List<> contains other class objects. How can this be done? Here are the details:
I have a fully populated List<> called List<TeamsSportsInfo>. See the TeamsSportsInfo class in first part of code below.
When just assigning this List as the datasource, the gridview successfully renders but only with columns for Id, Key, PublisherId and HomeSiteId. The properties in the other class objects, which are populated in the List<>, are obviously not automatically rendered.
So I have an overridden method for OnRowDataBound to try to manually populate these other fields but I'm not sure how to, when the type is a GridViewDataRow. Seems I have lost those values from the collection list.?.? And do I have to manually create these columns?
Also, in this overridden method, I need to look at the field values and have conditional statements determine the conferences and divisions so I know when to insert new header row and style them differently. Is the GridView the right control for this? I will have several custom links specific to each record too plus an edit button. Would a Repeater be more appropriate with some ListView or DataGrid inside it?
Looking for a working example of the appropriate control binding to a List<> of Class Object Lists.
Thanks.
I'm trying to bind a List<> to a gridview but it isn't working because the List<> contains other class objects. How can this be done? Here are the details:
I have a fully populated List<> called List<TeamsSportsInfo>. See the TeamsSportsInfo class in first part of code below.
When just assigning this List as the datasource, the gridview successfully renders but only with columns for Id, Key, PublisherId and HomeSiteId. The properties in the other class objects, which are populated in the List<>, are obviously not automatically rendered.
So I have an overridden method for OnRowDataBound to try to manually populate these other fields but I'm not sure how to, when the type is a GridViewDataRow. Seems I have lost those values from the collection list.?.? And do I have to manually create these columns?
Also, in this overridden method, I need to look at the field values and have conditional statements determine the conferences and divisions so I know when to insert new header row and style them differently. Is the GridView the right control for this? I will have several custom links specific to each record too plus an edit button. Would a Repeater be more appropriate with some ListView or DataGrid inside it?
Looking for a working example of the appropriate control binding to a List<> of Class Object Lists.
Thanks.
public class TeamsSportsInfo
{
//[teams] table
public Int32 Id { get; set; }
public String Key { get; set}
public Int32 PublisherId { get; set; }
public Int32 HomeSiteId { get; set; }
//other class objects needed for teams
public AffiliationSportsInfo AffiliationSportsInfo { get; set; }
public DisplayNamesSportsInfo DisplayNamesSportsInfo { get; set; }
public ParticipantsEventsSportsInfo ParticipantsEventsSportsInfo { get; set; }
}
<asp:GridView ID="gdvStats" runat="server" OnRowDataBound="gdvStats_RowDataBound"></asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
TeamsSportsBF teamsSportsBF = new TeamsSportsBF();
List<TeamsSportsInfo> listTeamsSportsInfo = teamsSportsBF.SelectTeamsList("sport_fullname, league_fullname, conf_fullname, div_fullname");
gdvStats.DataSource = listTeamsSportsInfo;
gdvStats.DataBind();
}
}
public void gdvStats_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Object row = e.Row.DataItem;
?
?
?
?
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER