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?
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() { }
}
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With