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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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() { }
}
Method 2
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; }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0