Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding List to GridView

I have a list of credit card objects. The credit card class is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Client
{
    public class CreditCard
    {
        public String A_Number;
        public String A_Name;
        public String A_Type;
        public String A_Owner_Type;
        public String Bank_City;
        public String Bank_State;
        public String Bank_ZIP;
        public String Balance;
        public String C_Username;

        public CreditCard()
        {

        }
    }
}

In another class, I am trying to bind the list to a grid view as follows:

protected void Page_Load(object sender, EventArgs e)
        {
            List<CreditCard> list = (List<CreditCard>)Session["list"];
            GridView_List.DataSource = list;
            GridView_List.DataBind();
        }

However, I am receiving the following error:

The data source for GridView with id 'GridView_List' did not have any properties or attributes from which to generate columns.  Ensure that your data source has content.

What is the problem? I checked that the list actually contains data so I don't know why it won't work? How can this problem be solved?

like image 510
Joe Borg Avatar asked Apr 15 '13 17:04

Joe Borg


2 Answers

You must use public properties for DataBinding. Update your class as follows:

  public class CreditCard
    {
        public String A_Number { get; set; }
        public String A_Name { get; set; }
        public String A_Type { get; set; }
        public String A_Owner_Type { get; set; }
        public String Bank_City { get; set; }
        public String Bank_State { get; set; }
        public String Bank_ZIP { get; set; }
        public String Balance { get; set; }
        public String C_Username { get; set; }

        public CreditCard() { }
    }
like image 75
d.moncada Avatar answered Oct 15 '22 06:10

d.moncada


You have defined your CreditCard as an object with fields. Data binding can only be done with properties. So, you need to do something like this for all fields:

public String A_Number { get; set; }
like image 20
Floremin Avatar answered Oct 15 '22 07:10

Floremin