Binding List of Custom Class to GridView or ListView Control

Scenario:

I am not going to write a lot but just show some code. Say you have a class(e.g. Customer) that itself exposes another class(e.g.Person) as its property. Now you want to take a List of Customer class and bind it to databound controls like GridView Or ListView ?

 public class Customer
{
    public Customer()
    {
        this.CustPerson = new Person();
    }
    public int id { get; set; }
    public Person CustPerson { get; set; }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Binding it to GridView:

Here is code that populates GridView with list of customers:

  List<Customer> customerlist = new List<Customer>();

        for (int i = 0; i <= 4; i++)
        {
            Customer cust = new Customer();
            cust.id = i;
            cust.CustPerson.FirstName = "John-" + i.ToString();
            cust.CustPerson.LastName = "Doe-" + i.ToString();
            cust.CustPerson.Age = 20 + i;
            customerlist.Add(cust);
        }

        GridView1.DataSource = customerlist;
        GridView1.DataBind();

Problem:

But say if your GridView is set to AutoGenerateColumns=true, which is default, then GV will show only the "id" column and the columns pertaining to Person are kind of lost.

GV with properties missing

How to access that inner class (e.g. Person here) ?

Answer to that is you need to explicitly cast the DataItem.Below is sample how you will display FirstName.

 <asp:TemplateField HeaderText="FirstName">
                     <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# ((Customer)Container.DataItem).CustPerson.FirstName %>'></asp:Label>
                    </ItemTemplate>
 </asp:TemplateField>

Similarly adding other columns the view will be something like below.

 

To get reference to DataItem in code-behind:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType== DataControlRowType.DataRow)
        {
            Customer drv = (Customer)e.Row.DataItem;

        }
    }

Conclusion:

Same concept goes with other databound controls like ListView. Hope it helps and let me know if you have any questions. 

Published Wednesday, April 28, 2010 6:00 PM by guru_sarkar
Filed under: ,

Comments

# re: Binding List of Custom Class to GridView or ListView Control

Wednesday, May 19, 2010 7:08 AM by Fouzia

Really good, people like me who are new to developing an item for an webpage.  

I am working on a page starting with creating the user table.  Now, question is how I can store the user profile information that user submits to the SQL SERVER 2008 table.  I will really appreciate your help.

Thank You.

# re: Binding List of Custom Class to GridView or ListView Control

Wednesday, May 19, 2010 10:59 AM by guru_sarkar

Fouzia,

Are you using your own database schema or default asp.net membership schema?

Check this: weblogs.asp.net/.../storing-user-profile-into-a-custom-table-using-createuser-wizard-control.aspx

If that's not what you are looking for please provide more information.

Can you share more details.

Alternatively, you can post your question here: http://forums.asp.net/25.aspx and post the link to your thread here and I will take a look.

# re: Binding List of Custom Class to GridView or ListView Control

Wednesday, May 26, 2010 1:21 PM by Joshua

I am attempting to bind a BindingList to a GridView. Like your sample, the BindingList is of a custom object called "Person". First, I allow the user to add records to the GridView using page TextBoxes and clicking a button. This works fine; the code is very similar to your add code. However, I am having trouble getting the grid to allow editing and removing records. I have set the BindingList "AllowEdit" and "AllowRemove" properties, but it seems that all of the events and everything involved must be handled manually in the code behind. Do you know of a simpler way to accomplish this? I have attempted using an ObjectDataSource because I read that it simplifies the editing and deleting, but my lack of familiarity with it caused me more problems than attempting it manually. If you could inform me of how to allow editing and deleting or provide another sample (whether a link or something similar to your code above), I would be very appreciative. Thanks.

# re: Binding List of Custom Class to GridView or ListView Control

Thursday, May 27, 2010 4:39 PM by guru_sarkar

Joshua,

---->Do you know of a simpler way to accomplish this?

I haven't played with BindingList much so I don't have anything useful for you.

If I get a chance I will look into it and see if I can update this post.

But for your trouble with ObjectDataSource check out some tutorials here that uses ODS:

www.asp.net/.../data

# re: Binding List of Custom Class to GridView or ListView Control

Friday, July 23, 2010 1:38 AM by Aravind Kumar

Hi,

I did the same example but its giving an error

CS0246: The type or namespace name 'Customer' could not be found (are you missing a using directive or an assembly reference?)

To solve this problem..

in .aspx file

       <asp:GridView ID="GridView1" runat="server" >

           <Columns>

           <asp:TemplateField HeaderText="FirstName">

               <ItemTemplate>

                   <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.CustPerson.FirstName") %>'></asp:Label>

               </ItemTemplate>

           </asp:TemplateField>

           </Columns>

       </asp:GridView>

in code behind file..

namespace WebApplication1

{

   public class Customer

   {

       public Customer()

       {

           this.CustPerson = new Person();

       }

       public int id { get; set; }

       public Person CustPerson { get; set; }

   }

   public class Person

   {

       public string FirstName { get; set; }

       public string LastName { get; set; }

       public int Age { get; set; }

   }

   public partial class WebForm2 : System.Web.UI.Page

   {

       protected void Page_Load(object sender, EventArgs e)

       {

           List<Customer> customerlist = new List<Customer>();

           for (int i = 0; i <= 4; i++)

           {

               Customer cust = new Customer();

               cust.id = i;

               cust.CustPerson.FirstName = "John-" + i.ToString();

               cust.CustPerson.LastName = "Doe-" + i.ToString();

               cust.CustPerson.Age = 20 + i;

               customerlist.Add(cust);

           }

           GridView1.DataSource = customerlist;

           GridView1.DataBind();

       }

   }

}

because mostly we don't maintain user defined classes in side

"public partial class WebForm2 : System.Web.UI.Page"

you did really nice job...

Thank you...

# re: Binding List of Custom Class to GridView or ListView Control

Friday, July 23, 2010 11:21 AM by guru_sarkar

Thanks Aravind Kumar for clarification.

Yes, usually we don't include classes inside Page's code-behind. So you have to add reference to assembly and namespace depending on where you define your class.

But for this case my classes were not inside a namespace and the class files are inside App_Code folder and so it works without any error.

# re: Binding List of Custom Class to GridView or ListView Control

Thursday, February 24, 2011 11:26 AM by GMann

Thanks for the hint !

Is there a book / MSDN article that explains this in greater detail, ie biding a generic list with a data control ? I found that if I set my gridview autoGenerateColumns property to TRUE that my data was displayed.

# re: Binding List of Custom Class to GridView or ListView Control

Friday, February 25, 2011 1:21 PM by guru_sarkar

GMann,

I don't know where I exactly got this from but here is a basic GV Databinding article if that helps: highoncoding.com/.../206_GridView_DataBinding.aspx

If your GV displays data with AutoGenerateColumns=true, may be your class isn't having any property of a custom type.

Or I am completely misunderstanding your question.

# re: Binding List of Custom Class to GridView or ListView Control

Wednesday, March 30, 2011 4:57 AM by Raj

Can we use the same concept for BoundField with some tweaking rather than TemplateFields?

# re: Binding List of Custom Class to GridView or ListView Control

Wednesday, March 30, 2011 11:46 AM by guru_sarkar

@Raj:

No, not in .aspx markup. You will have to do that in code behind / RowDataBound event and set value by e.Row.Cells[i].Text = some value...

# re: Binding List of Custom Class to GridView or ListView Control

Tuesday, October 11, 2011 10:41 AM by radu

is there a way to accomplish this from the c# code in some event? i'm trying to bind a custom class to a grid view which is in a web control in a separate dll

# re: Binding List of Custom Class to GridView or ListView Control

Tuesday, October 11, 2011 11:13 AM by guru_sarkar

radu,

Not getting your questuon right but check the RowDataBound event in the last code block above.

# re: Binding List of Custom Class to GridView or ListView Control

Tuesday, November 8, 2011 1:17 AM by sivaguru

Error 17 The type or namespace name 'Listdatabind' could not be found (are you missing a using directive or an assembly reference?) D:\3Z\3Z\AddRole.aspx.cs 22 5 D:\3Z\3Z\

# re: Binding List of Custom Class to GridView or ListView Control

Tuesday, November 8, 2011 11:37 AM by guru_sarkar

@sivaguru,

What is Listdatabind in your code? Can you share your code?

# re: Binding List of Custom Class to GridView or ListView Control

Friday, April 27, 2012 3:19 AM by Manoj Batra

suppose i don't know the no of columns in my gridview it could be 5,10 or even 50 and i set autogeneratecolumns property to true  then , can i use these getter setter method of the person class in the above example.

# re: Binding List of Custom Class to GridView or ListView Control

Tuesday, January 7, 2014 4:28 PM by Ken

Actually I am looking for how to use BindingList<T> with an Object Collection where MyObject incorporates IList so I would like MyObects[] to be databound to the BindingList can anyone explain how to do that - the Ilist in each object has values such as Boolean, string, int , float that need to be exposed - I can do this with myObject but how to do it with myObjects[].

Leave a Comment

(required) 
(required) 
(optional)
(required)