【问题标题】:Assigning List<class> to a GridView as DataSource gives error将 List<class> 作为 DataSource 分配给 GridView 会出错
【发布时间】:2015-10-04 00:16:24
【问题描述】:
  1. 我创建了一个返回集合类的类 Car
  2. 在 Page_Load 事件中,我正在调用 Cars 类中的函数来获取结果。并将结果分配给 GridView。

这样做会给我带来错误-“ID 为 'gv' 的 GridView 的数据源没有任何属性或属性可用于生成列。确保您的数据源有内容。”

这是我的汽车课-

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>

【问题讨论】:

  • 发布你的gridview的定义。
  • ...任何属性或属性...
  • @FelipeOriani GridView 将自动生成列。但是,我已经更新了问题。

标签: c# asp.net gridview


【解决方案1】:

尝试使用属性,而不仅仅是公共属性。您还可以使用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

【讨论】:

  • 这对我有用。但它应该返回 4 列。即“型号”、“车型”、“配件”和“价格”。但只返回 3。“附件”列未出现在 GridView 中。有什么想法吗?
  • 这是另一个列表,您必须有另一个网格才能显示此列表。
  • 我正在考虑使用另一个 GridView 在同一行中显示此内容,但我不确定如何执行此操作。可以举个例子吗?
  • 你可以试试master/detail,例如:msdn.microsoft.com/en-us/library/aa581796.aspx
  • master/detail 是不可能的。我在记录中没有主键。有没有办法在同一行中显示附件列以及其他 3 列
【解决方案2】:

这里有一个解决方案。引擎要生成的列应该可以找到任何可访问的属性。

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; }

【讨论】:

  • 它有效。但它只将“Model”、“CarType”和“Price”列绑定到 GridView。为什么附件没有绑定?
  • 错误说 - “任何属性或属性”。在这里,您已经创建了属性。什么是属性,我们可以在这里使用它们吗?
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 2013-04-12
  • 2010-12-03
相关资源
最近更新 更多