将List <class>分配给GridView作为DataSource会出错

时间:2015-10-03 12:26:04

标签: c# asp.net gridview

  1. 我创建了一个返回集合类的 Car 类。
  2. 在Page_Load事件中,我调用Cars类中的函数来获取结果。并将结果分配给GridView。
  3. 这样做会给我带来错误 - &#34;带有id&#39; gv&#39;的GridView的数据源没有任何属性或属性来生成列。确保您的数据源包含内容。&#34;

    这是我的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>
    

2 个答案:

答案 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