ASP.NET 3.5 Gridview DataBind to Generic Class Object List


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.

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;

        public void gdvStats_RowDataBound(Object sender, GridViewRowEventArgs e)
            Object row = e.Row.DataItem;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to cast e.Row.DataItem to the TeamsSportsInfo to manipulate it i.e.

TeamsSportsInfo row =(TeamsSportsInfo) e.Row.DataItem;

in markup you can try like this:
                        <asp:Label ID="Label2" runat="server" Text='<%# ((TeamsSportsInfo)Container.DataItem).AffiliationSportsInfo.SomeProperty%>'></asp:Label>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kruegersteAuthor Commented:
Thanks, works great.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.