这样做会给我带来错误 - "带有id' gv'的GridView的数据源没有任何属性或属性来生成列。确保您的数据源包含内容。"
这是我的Car class -
class Car
{
public Car()
{
}
public string Model;
public string CarType;
public List<Accessories> Accessory=new List<Accessories>();
public string Price;
public List<Car> GetCars()
{
// Car 1
Car Car1 = new Car();
Car1.Model = "Range Rover Evoque";
Car1.CarType = "SUV";
Car1.Price = "70 Lac";
Car.Accessories car1Interior = new Car.Accessories
{
LetherSeats = "Front and Back",
Music = "CD Player",
GPS = "Touch Screen"
};
Car.Accessories car1Ext = new Car.Accessories
{
LetherSeats = "Front Only",
Music = "No",
GPS = "No"
};
Car1.Accessory.Add(car1Interior);
Car1.Accessory.Add(car1Ext);
// Car 2
Car Car2 = new Car();
Car2.Model = "Lamborghini Sesto Elemento";
Car2.CarType = "Racing";
Car.Accessories car2Interior = new Car.Accessories
{
LetherSeats = "Front and Back",
Music = "None",
GPS = "Touch Screen"
};
Car.Accessories car2Ext = new Car.Accessories
{
FogLights = "None",
Spolier = "Yes",
NeonLight = "Yes"
};
Car2.Accessory.Add(car2Interior);
Car2.Accessory.Add(car2Ext);
Car2.Price = "2 Crore";
List<Car> cars = new List<Car>();
cars.Add(Car1);
cars.Add(Car2);
return cars;
}
public class Accessories
{
private string _LeatherSeats;
private string _GPS;
private string _Music;
private string _FogLights;
private string _Spoiler;
private string _NeonLight;
public string LetherSeats { get { return _LeatherSeats; } set { _LeatherSeats = value; } }
public string GPS { get { return _GPS; } set { _GPS = value; } }
public string Music { get { return _Music; } set { _Music = value; } }
public string FogLights { get { return _FogLights; } set { _FogLights = value; } }
public string Spolier { get { return _Spoiler; } set { _Spoiler = value; } }
public string NeonLight { get { return _NeonLight; } set { _NeonLight = value; } }
}
}
这是Page_Load方法 -
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Car wr = new Car();
gv.DataSource = wr.GetCars();
gv.DataBind();
}
}
ASPX页面 -
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server"></asp:GridView>
</div>
</form>
答案 0 :(得分:1)
这是一个解决方案。要生成引擎的列应找到任何可访问的属性。
private List<Accessories> _accessory;
public string Model { get; set; }
public string CarType { get; set; }
public List<Accessories> Accessory
{
get { return _accessory ?? (__accessory = new List<Accessories>();)}
}
public string Price { get; set; }
答案 1 :(得分:1)
尝试使用属性,而不仅仅是public属性。您还可以使用static
方法获取列表,因为它不是Car
对象的一部分。样本:
public class Car
{
public Car()
{
Acessory = new List<Accessories>();
}
public string Model { get; set; }
public string CarType { get; set; }
public List<Accessories> Accessory { get; set; }
public string Price { get; set; }
public static List<Car> GetCars()
{
// Car 1
Car Car1 = new Car();
Car1.Model = "Range Rover Evoque";
Car1.CarType = "SUV";
Car1.Price = "70 Lac";
Car.Accessories car1Interior = new Car.Accessories
{
LetherSeats = "Front and Back",
Music = "CD Player",
GPS = "Touch Screen"
};
Car.Accessories car1Ext = new Car.Accessories
{
LetherSeats = "Front Only",
Music = "No",
GPS = "No"
};
Car1.Accessory.Add(car1Interior);
Car1.Accessory.Add(car1Ext);
// Car 2
Car Car2 = new Car();
Car2.Model = "Lamborghini Sesto Elemento";
Car2.CarType = "Racing";
Car.Accessories car2Interior = new Car.Accessories
{
LetherSeats = "Front and Back",
Music = "None",
GPS = "Touch Screen"
};
Car.Accessories car2Ext = new Car.Accessories
{
FogLights = "None",
Spolier = "Yes",
NeonLight = "Yes"
};
Car2.Accessory.Add(car2Interior);
Car2.Accessory.Add(car2Ext);
Car2.Price = "2 Crore";
List<Car> cars = new List<Car>();
cars.Add(Car1);
cars.Add(Car2);
return cars;
}
public class Accessories
{
private string _LeatherSeats;
private string _GPS;
private string _Music;
private string _FogLights;
private string _Spoiler;
private string _NeonLight;
public string LetherSeats { get { return _LeatherSeats; } set { _LeatherSeats = value; } }
public string GPS { get { return _GPS; } set { _GPS = value; } }
public string Music { get { return _Music; } set { _Music = value; } }
public string FogLights { get { return _FogLights; } set { _FogLights = value; } }
public string Spolier { get { return _Spoiler; } set { _Spoiler = value; } }
public string NeonLight { get { return _NeonLight; } set { _NeonLight = value; } }
}
}
在你的页面中,你可以试试这个:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv.DataSource = Car.GetCars();
gv.DataBind();
}
}
由于您有一个集合属性,因此无法在列中显示它,因为您可以拥有多个项目。在这种情况下,嵌套GridView可以帮助您,但您必须在gridview中定义属性。请参阅此示例:
http://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx